/** * init_peer(peer_t * peer, int id, char * ip, unsigned short port) -> int * * * initialize the peer_t structure peer with an id, ip address, and a * port. Further, it will set up the sockaddr such that a socket * connection can be more easily established. * * Return: 0 on success, negative values on failure. Will exit on bad * ip address. * **/ int init_peer(peer_t *peer, char * id, char * ip, unsigned short port){ struct hostent * hostinfo; //set the host id and port for referece peer->port = port; //get the host by name if((hostinfo = gethostbyname(ip)) == NULL){ perror("gethostbyname failure, no such host?"); herror("gethostbyname"); exit(1); } //zero out the sock address bzero(&(peer->sockaddr), sizeof(peer->sockaddr)); //set the family to AF_INET, i.e., Internet Addressing peer->sockaddr.sin_family = AF_INET; //copy the address to the right place bcopy((char *) (hostinfo->h_addr), (char *) &(peer->sockaddr.sin_addr.s_addr), hostinfo->h_length); //encode the port peer->sockaddr.sin_port = htons(port); //printf("peer ip address : %s\npeer port number : %d\n",(char *)inet_ntoa(peer->sockaddr.sin_addr),port); calc_id((char *)inet_ntoa(peer->sockaddr.sin_addr),port,id); memcpy(peer->id, id, ID_SIZE); return 0; }
/** * init_seeder() documentation TO DO **/ void init_seeder(bt_args_t *bt_args) { char *parse_bind_str; // temporary string char *token; char delim[] = ":"; // delimiter char *ip; // to store IP addr char id[20]; // to store SHA1 hash into unsigned short port; // to store port int i; // loop iterator variable // copy bt_args.bind_info to parse_bind_str to avoid losing original bt_args.bind_info parse_bind_str = malloc(strlen(bt_args->bind_info) + 1); memset( parse_bind_str, 0x00, (strlen(bt_args->bind_info) + 1) ); // zero-out parse_bind_str strncpy(parse_bind_str, bt_args->bind_info, strlen(bt_args->bind_info)); // printf("testing, parse_bind_str: '%s'\n", parse_bind_str); for ( token = strtok(parse_bind_str, delim), i = 0; token; token = strtok(NULL, delim), i++ ) { switch(i) { case 0: ip = token; break; case 1: port = atoi(token); break; default: break; } } if (i < 2) { // too few arguments after '-b' flag fprintf(stderr, "ERROR: Not enough values in '%s'\n", bt_args->bind_info); usage(stderr); exit(1); } if (token) { // token should be NULL by this time fprintf(stderr, "ERROR: Too many values in '%s'\n", bt_args->bind_info); usage(stderr); exit(1); } // calculate SHA1 hash of the concatenation of binding machine's IPaddr and port calc_id(ip, port, id); // calculate bt client's ID // printf("testing, inside init_seeder, before putting in bt_args, id without hex: '%s'\n", id); memcpy(bt_args->id, id, 20); make_seeder_listen(ip, port, bt_args); /* make seeder listen to incoming leecher connections */ }
int __verify__(char *fname, char first, char *name, char *chaff, char *id, char *hash, char *ip, unsigned short port){ int failed = FALSE; //start verification if ((first & 0x13) != 0x13){ printf("Failed on the first byte.\n"); failed = TRUE; return FALSE; } if (strncmp(name, "BitTorrent Protocol", 19) != 0){ printf("Failed on Protocol Name \n"); failed = TRUE; return FALSE; } if (strncmp(chaff, "00000000", 8) != 0){ printf("Failed on chaff buffer\n"); failed = TRUE; return FALSE; } //SHA1 hash of filename for info-hash unsigned char info_hash[20]; SHA1((unsigned char *) fname, strlen(fname), info_hash); if (memcmp(hash, info_hash, 20) != 0){ printf("failed on the file hash\n"); return FALSE; } char id_val[20]; calc_id(ip, INIT_PORT, id_val); if (memcmp(id, id_val, 20) != 0){ failed = TRUE; } return TRUE; }
// Open a listening port // (the main user interaction) // Shout-out the calculated EventID on establishing the connection // Shout-out again on any incoming packet void deliver_id() { int sock, con; int yes = 1, i, c; struct addrinfo hints, *svr, *p; ID_t myid; struct sockaddr_in client_addr, serv; socklen_t sin_size; char s[INET_ADDRSTRLEN]; char msg[100]; sock = socket(AF_INET, SOCK_STREAM, 0); if (sock < 0) { fprintf(stderr, "deliver_id() socket failed %d\n", errno); return; } c = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char *)&yes, sizeof(int)); if (c < 0) { fprintf(stderr, "deliver_id() setsockopt failed %d\n", errno); return; } serv.sin_family = AF_INET; serv.sin_addr.s_addr = INADDR_ANY; serv.sin_port = htons(OUR_PORT); c = bind(sock, (struct sockaddr*)&serv, sizeof(serv)); if (c < 0) { close(sock); fprintf(stderr, "deliver_id() bind failed %d\n", errno); return; } c = listen(sock, 1); if (c < 0) { fprintf(stderr, "deliver_id() listen failed %d\n", errno); return; } printf("waiting for connections...\n"); for (;;) { // main accept() loop sin_size = sizeof(client_addr); con = accept(sock, (struct sockaddr*)&client_addr, &sin_size); if (con < 0) { fprintf(stderr, "deliver_id() accept failed %d\n", errno); break; } #ifdef WIN32 getnameinfo((const sockaddr*)&client_addr, sizeof(client_addr), s, INET_ADDRSTRLEN, NULL, 0, 0); #else inet_ntop(AF_INET, (const void*)(&client_addr.sin_addr), s, INET_ADDRSTRLEN); #endif printf("Connected to %s\n", s); do { // loop for repeated shouts within one connect calc_id(&myid); make_id_str(msg, sizeof(msg), myid); c = send(con, msg, strlen(msg), MSG_NOSIGNAL); if (c < 0) { close(con); if (errno != ECONNRESET && errno != EPIPE) { fprintf(stderr, "deliver_id() send failed %d\n", errno); break; } } c = recv(con, msg, sizeof(msg), 0); #ifdef WIN32 } while (c > 0); #else while (c>0) c = recv(con, msg, sizeof(msg), MSG_DONTWAIT); // flush input queue } while (errno == EAGAIN || errno == EWOULDBLOCK); #endif printf("Connection dropped\n"); }
// collects continuously all data from the EventID server // and stores it in our ringbuffer // // runs in a sub-thread static void* ID_collect(void* nyx) { char buff[ID_MESSAGE_SIZE]; unsigned long int id; long long int offset; struct timeval tv; int sock, x; ID_t myid; unsigned long int id_iq, id_sub; for(;;) { // loop over several reconnects sock = ID_connect(); if (sock < 0) { // Maybe try to reconnect etc pp recon_retry++; if (recon_retry > 20) { fprintf(stderr, "ID_collect() giving up to reconnect\n", errno); #ifdef WIN32 WSACleanup(); #endif exit(0); } sleep(1); continue; } for(;;) { // loop over several msgs x = recv(sock, buff, ID_MESSAGE_SIZE, 0); gettimeofday(&tv, NULL); if (x < 0) { // timeout ... check if still connected // and/or maybe reconnect close(sock); // lazy... connected = 0; break; } else if (x == ID_MESSAGE_SIZE) { // we dont want these big messages, just drop it // and wait for the next one continue; } // decode message and put into events array // "151016 090505.543 45C7A4D\r\n" // find first blank backwards from end for (;--x>0;) if (buff[x] == ' ') break; if (x <= 0) { // error reading packet, just ignore it DEBUG(printf("skipping unparsable message\n")); continue; } sscanf(buff+x, "%lx", &id); // (T-O) / 0.1 = ID (based on T in seconds) // O = T - 0.1 * ID offset = (unsigned long long int) tv.tv_usec + 1000000 * (unsigned long long int) tv.tv_sec - 100000 * (unsigned long long int) id; DEBUG(printf("ID %2d found %lx -=> Offset %lld\n", events_idx, id, offset)); // fill all buffer entries with the first ID we receive // this alleviates us of several nasty sanity checks ;) if (first_entry) { first_entry = 0; for (events_idx = ID_EVENT_WINDOW_SIZE; --events_idx;) { events[events_idx].t.tv_sec = tv.tv_sec; events[events_idx].t.tv_usec = tv.tv_usec; events[events_idx].id = id; events[events_idx].offset = offset; } printf("Connected to EventID server and getting data\n"); } events[events_idx].t.tv_sec = tv.tv_sec; events[events_idx].t.tv_usec = tv.tv_usec; events[events_idx].id = id; events[events_idx].offset = offset; events_idx = (events_idx + 1) % ID_EVENT_WINDOW_SIZE; connected = 1; recon_retry = 0; DEBUG(calc_id(&myid)); DEBUG(make_id_str(buff, sizeof(buff), myid)); DEBUG(printf("%s", buff)); } // end of forever (mesgs) } // end of forever (reconnects) }
int main(int argc, char **argv) { fd_set rdfs; int s, t; struct sockaddr_can addr; struct can_filter rfilter; struct can_frame frame; int testcase = 0; int nbytes, ret; struct ifreq ifr; int ifindex; if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) { perror("socket"); return 1; } if ((t = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) { perror("socket"); return 1; } strcpy(ifr.ifr_name, "vcan0"); ioctl(s, SIOCGIFINDEX, &ifr); ifindex = ifr.ifr_ifindex; addr.can_family = AF_CAN; addr.can_ifindex = ifindex; if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) { perror("bind"); return 1; } if (bind(t, (struct sockaddr *)&addr, sizeof(addr)) < 0) { perror("bind"); return 1; } rfilter.can_id = 0xF; /* receive only the filter requests */ rfilter.can_mask = CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG; setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter)); /* disable default receive filter on the test socket */ setsockopt(t, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0); while (1) { FD_ZERO(&rdfs); FD_SET(s, &rdfs); FD_SET(t, &rdfs); if ((ret = select(t+1, &rdfs, NULL, NULL, NULL)) < 0) { perror("select"); break; } if (FD_ISSET(s, &rdfs)) { if ((nbytes = read(s, &frame, sizeof(struct can_frame))) < 0) { perror("read"); exit(1); } if (nbytes < sizeof(struct can_frame)) { fprintf(stderr, "read: incomplete CAN frame\n"); exit(1); } if ((frame.can_id != 0xF) || (frame.can_dlc != 1)) { fprintf(stderr, "\nWrong request from master!\n"); exit(1); } testcase = frame.data[0]; if (testcase < 18) { rfilter.can_id = calc_id(testcase); rfilter.can_mask = calc_mask(testcase); setsockopt(t, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter)); printf("testcase %2d : can_id = 0x%08X can_mask = 0x%08X\n", testcase, rfilter.can_id, rfilter.can_mask); } frame.can_id = 0xFA; /* filter ack */ if (write(s, &frame, sizeof(frame)) < 0) { perror("write"); exit(1); } if (testcase > 17) break; } if (FD_ISSET(t, &rdfs)) { if ((nbytes = read(t, &frame, sizeof(struct can_frame))) < 0) { perror("read"); exit(1); } if (nbytes < sizeof(struct can_frame)) { fprintf(stderr, "read: incomplete CAN frame\n"); exit(1); } printf ("%08X\n", frame.can_id); } } printf("testcase %2d : Filtertest done.\n", testcase); close(s); close(t); return 0; }
void seeder(bt_args_t *bt_args,bt_info_t* my_bt_info) { char handshakebuffer[68]; struct sockaddr_in servAddr, cli_addr; int n; char buffer[MAX_LENGTH]; char seederid[20]; struct bt_handshake seeder_handshake; char bytes[100]; char tempbuf[100]; char leecher_handshake[68]; //int * bytesreaded= malloc(sizeof(int)); int bytesreaded=0; memset(&servAddr, 0, sizeof(servAddr)); servAddr.sin_family = AF_INET; servAddr.sin_addr.s_addr = INADDR_ANY; servAddr.sin_port = htons(INIT_PORT); //some issue here //char* tbuff= malloc(bt_args->bt_info->piece_length); //char* tbuff= malloc(sizeof(char)* bt_args->bt_info->piece_length); //memset(tbuff, 1, bt_args->bt_info->piece_length); char tbuff[bt_args->bt_info->piece_length]; //Prepare handshake structure of seeder printf("\n %d size of seeder \n ",sizeof(seeder_handshake)); strcpy(seeder_handshake.protocol_id,"19BitTorrentProtocol"); printf("\n 19BitTorrentProtoco Copied"); strcpy(seeder_handshake.reserved,"00000000"); printf("\n 00000000 copied"); char* fileHash = compute_hash_for_fileName(bt_args->bt_info->name); strcpy(seeder_handshake.info_hash,fileHash); calc_id("127.0.0.1",INIT_PORT,seederid); // issue here (peer_id is not matched) htons is removed strcpy(seeder_handshake.peer_id,seederid); printf(" \n peer id of seeder is %s \n",seederid); //copy into buffer memset(&handshakebuffer, 0, sizeof(servAddr)); memcpy(handshakebuffer,&seeder_handshake,68); //printf(" \n seeder peer id is %s",seederid); //printf("\n %d bytes from structure ",strlen(handshakebuffer)); printf("\n %d size of seeder \n ",sizeof(seeder_handshake)); //printf("\n seeder Port after intializing is %d",ntohs(servAddr.sin_port)); int seederfd=socket(AF_INET,SOCK_STREAM,0); if(seederfd==-1) { perror("socket"); exit(1); } // printf(" \n socket created and its value is %d \n",seederfd); // printf("\n seeder Port is %d",ntohs(servAddr.sin_port)); // printf(" \n sockaddr size is %d",sizeof(struct sockaddr_in)); // printf(" \n seeder peer size is %d",sizeof(servAddr)); // printf("\n seeder IP is %s",inet_ntoa(servAddr.sin_addr)); // printf(" \n socket created"); if (bind(seederfd, (struct sockaddr*) &servAddr, sizeof(servAddr))>0) { perror("Bind"); exit(1); } listen(seederfd,5); while(1) { //Accept incoming connections socklen_t clntAddrLen = sizeof(cli_addr); int newsockfd = accept(seederfd,(struct sockaddr *) &cli_addr, &clntAddrLen); if (newsockfd < 0) { perror("error on accept"); close(seederfd); exit(1); } //printf("\n \n IP is %s",inet_ntoa(cli_addr.sin_addr)); //printf("\n Port is %d",ntohs(cli_addr.sin_port)); //printf("\n IP is %s",inet_ntoa(cli_addr.sin_addr)); n = read(newsockfd,leecher_handshake,68); //Read handshake message if (n < 0) { perror("ERROR reading from socket"); exit(1); } printf("\n %d bytes received at seeder end \n",n); printf("\n sent from leecher :%s ",leecher_handshake); printf(" \n \n buffer copied from structure is : %s \n",handshakebuffer); printf("\n %d size of seeder \n ",sizeof(seeder_handshake)); // Fill in the handshake structure and compare memcpy(tempbuf, leecher_handshake, 20); printf(" temp buf:%s \n",tempbuf); if(strcmp(tempbuf,seeder_handshake.protocol_id)>0) { printf("Not a bittorrent protocol \n\n\n"); write (newsockfd,"Not a bittorrent protocol",100); close(newsockfd); //exit(1); } else { printf(" \n bit torrent handshake header ok"); } memset(&tempbuf,0,sizeof(tempbuf)); memcpy(tempbuf, leecher_handshake+20,8); printf(" temp buf:%s \n",tempbuf); printf("\n sent from leecher :%s \n",seeder_handshake.reserved); if(strcmp(tempbuf,seeder_handshake.reserved)>0) { printf("Info hash value not matched.Server dont have the file"); write (newsockfd,"",100); close(newsockfd); //exit(1); } else { printf(" \n bit torrent Resserve field ok"); } memset(&tempbuf,0,sizeof(tempbuf)); memcpy(tempbuf, leecher_handshake+28, 20); if(strcmp(tempbuf,seeder_handshake.info_hash)>0) { printf("Info hash value not matched.Server dont have the file"); write (newsockfd,"Info hash value not matched.Seedert have the file",100); close(newsockfd); //exit(1); } else { printf(" \n bit torrent Info Hash ok"); } memset(&tempbuf,0,sizeof(tempbuf)); memset(&leecher_handshake,0,sizeof(leecher_handshake)); //end the handshake message n = write(newsockfd,handshakebuffer,68); if (n < 0) { perror("ERROR writing to socket"); exit(1); } printf("\n %d bytes sent to client",n); //Clear the handshake buffer memset(&handshakebuffer,0,sizeof(tempbuf)); // printf(" \n \n Reached end of seeder handshake "); while(1) { int piece_id=0; // printf("\n no data received upto now"); // wait for leecher to respond n = read(newsockfd,&piece_id,sizeof(piece_id)); //Read piece id message if (n < 0 ) { perror("ERROR reading from socket"); exit(1); } if (n>0) { printf("\n Data sent by receiver after handsake is %d",ntohl(piece_id)); // printf("\n Data sent by receiver after handsake is %d",piece); printf("\n fetching data for given piece : %d\n",ntohl(piece_id)); //fcntl(newsockfd, F_SETFL, O_NONBLOCK); //char* buf2=read_piece_from_file(bt_args->bt_info->name,1,bt_args->bt_info->piece_length,bt_args->bt_info->my_peiecehases,&bytesreaded); // char* buf2=read_piece_from_file(bt_args->bt_info->name,ntohl(piece_id),bt_args->bt_info->piece_length,bt_args->bt_info->my_peiecehases,&bytesreaded,tbuff); char* buf2=read_piece_from_file(bt_args->bt_info->name,ntohl(piece_id),bt_args->bt_info->piece_length,bt_args->bt_info->my_peiecehases,&bytesreaded); //printf(" Buff Readed %s",buf); //char* buf ="qqqqqqaddasdad"; //bytesreaded=sizeof(buf); // tbuff=nreadmk(); //printf(" tbuff %s \n ",tbuff); char * mybuff="Mohammed"; printf("\n wrting it into socket for leecher"); int s=0; while (s<bytesreaded) { n=write(newsockfd,buf2,bytesreaded); s=s+n; printf("\n %d bytes sent from seeder for piece %d \n",n,ntohl(piece_id)); sleep(3); } } } // end of while } //end of while }
void client(bt_args_t *bt_args,bt_info_t* my_bt_info,char* bitfilename) { struct bt_handshake handshake; int i,n; char handshake_str[68]; char leecher[20]; char seeder[20]; char *seederfromhandshake; char tempbuf[20]; int sockfd; char receivedbuf_handshake[68]; char* readbuf; //creating scoket int max_chunck=bt_args->bt_info->num_pieces; //char* bitfilename="Restart.tmp"; char* bit_down = malloc(sizeof(char)*max_chunck); bit_down=initialize_read_bitfieldfile(bitfilename,max_chunck); printf(" bitdown after initial %s \n",bit_down); if((sockfd=socket(AF_INET,SOCK_STREAM,0))== -1) { perror("socket"); exit(1); } // print_peer(bt_args->peers[1]); //printf(" \n sockaddr size is %d",sizeof(struct sockaddr_in)); //printf(" \n peer size is %d",sizeof(bt_args->peers[1]->sockaddr)); //printf("\n IP is %s",inet_ntoa(bt_args->peers[1]->sockaddr.sin_addr)); //printf("\n Port is %d",ntohs(bt_args->peers[1]->sockaddr.sin_port)); //Initialize the structure if(connect(sockfd,(struct sockaddr *) &(bt_args->peers[1]->sockaddr),sizeof(struct sockaddr_in)) < 0) { perror("connect"); exit(1); } //Preparing handshake message strcpy(handshake.protocol_id,"19BitTorrentProtocol"); strcpy(handshake.reserved,"00000000"); //printf("fileName to compute the hash : %s \n",bt_args->bt_info->name); char * fileHash = compute_hash_for_fileName(bt_args->bt_info->name); //printf("Handshake fileHash : %s , len of filehash %i\n",fileHash,strlen(fileHash)); strcpy(handshake.info_hash,fileHash); //printf(" Host : %s \n ",bt_args->peers[1]->id); //printf(" Host client Port : %d \n ",bt_args->peers[1]->port); calc_id("127.0.0.1",bt_args->peers[1]->port,leecher); strcpy(handshake.peer_id,leecher); printf(" \n peer id of leeecher is %s",leecher); //printf("\n handshake intialiazed"); memset(&leecher,0,sizeof(leecher)); memset(&handshake_str,0,sizeof(handshake_str)); memcpy(handshake_str,&handshake,68); //printf(" buffer copied from structure is : %s \n",handshake_str); //printf("size of handshake_str is %d",strlen(handshake_str)); printf("handshake_str is intialized"); //send handshake n=write(sockfd,handshake_str,68); if(n<=0){ perror("write failed"); exit(1); } printf("\n %d bytes sent to server",n); //clear the sent buffer memset(&handshake_str,0,sizeof(handshake_str)); memset(&handshake,0,sizeof(handshake)); //receive handshake while(1) { printf("\n sockfd at handshake is %d",sockfd); n=read(sockfd,receivedbuf_handshake,68); if(n<0) { perror("read failed"); exit(1); } printf("\n received buffer from seeder is %s , length : %d",receivedbuf_handshake ,sizeof(receivedbuf_handshake)); printf(" \n \n Handshake response is %s \n",receivedbuf_handshake); printf(" \n seeder port taken is %d \n ",bt_args->peers[1]->port); //calculating peer id of leecher char ip4[INET_ADDRSTRLEN]; // space to hold the IPv4 stri inet_ntop(AF_INET,&(bt_args->peers[1]->sockaddr.sin_addr),ip4, INET_ADDRSTRLEN); printf("The IPv4 address is: %s\n", ip4); calc_id(ip4,bt_args->peers[1]->port,seeder); //need to update this with seeder ip //Fetch the peer id from buffer printf(" Seeder Peer ID %s \n",seeder); memset(&tempbuf,0,sizeof(tempbuf)); printf("\n sockfd at peer id verification handshake is %d",sockfd); memcpy(tempbuf,receivedbuf_handshake+48,20); tempbuf[20]=NULL; printf(" Seeder Peer ID received is : %s .length %d\n",tempbuf,sizeof(tempbuf)); printf(" Seeder peer id calculated is :%s .length %d",seeder,sizeof(seeder)); printf("\n sockfd stored in struct \n"); if(strcmp(tempbuf,seeder)>0){ printf(" peer_id of seeder not expected.So dont want to continue to download\n"); exit(1); } //Clear all the buffers and char arrays used. memset(&receivedbuf_handshake,0,sizeof(receivedbuf_handshake)); memset(&tempbuf,0,sizeof(tempbuf)); printf("\n sockfd after handshake is %d",sockfd); printf("%d",bt_args->bt_info->num_pieces); printf("Handshake Complete"); //break; /* verbose mode if() printf("\nReceived handshake from: %s\n", bt_args.peers[i]->ip);*/ //peer_chunk=find_piece_number_to_download(); printf("%d",bt_args->bt_info->num_pieces); printf("Start call random_pick\n"); int piece_length=bt_args->bt_info->piece_length; printf(" Length of piece: %ld",piece_length); long plength=bt_args->bt_info->piece_length; char* phashs=bt_args->bt_info->my_peiecehases; printf(" Length of piece: %ld",piece_length); //random_pick(sockfd,plength,phashs,max_chunck); char * filenamew="TempTemp1.txt"; if (bt_args->save_file != NULL) filenamew= bt_args->save_file; int piece_id; long sizefiletillnow=0; for (piece_id=1;piece_id <=max_chunck; piece_id=piece_id+1) // for (piece_id = max_chunck ; piece_id >= 1; piece_id=piece_id-1) { char* nbitdown=initialize_read_bitfieldfile(bitfilename,max_chunck); printf("\n str nbitdown %s ---\n",nbitdown); if (nbitdown[piece_id-1]=='Y') { printf("this piece already downloaded before\n"); sizefiletillnow=sizefiletillnow+plength; continue; } int data = htonl(piece_id); char tbuf[plength]; if(write(sockfd,&data,sizeof(data))>0) { if (plength > bt_args->bt_info->length) { plength=bt_args->bt_info->length; } char myrbuf[plength]; int s=0; while (s<plength && sizefiletillnow< bt_args->bt_info->length ) { n=read(sockfd,tbuf,plength-s); sizefiletillnow=sizefiletillnow+n; if (s<plength) { memcpy(&myrbuf[s],tbuf,n); } s=s+n; printf(" NUMBER OF BYTES READED %i \n",n); printf(" Size of file %i and dowloaded till now %i \n",bt_args->bt_info->length,sizefiletillnow); } printf(" Total NUMBER OF BYTES READED %i \n",s); printf(" now we reading from server on sockfd %i \n",sockfd); printf(" NUMBER OF BYTES READED %i \n",n); printf("Recieved Buffer is %s",myrbuf); if(n<0) { perror("read failed"); exit(1); } printf("\n\n\n Wiritng to given file for piece_id %i \n",piece_id); printf("\n\n\n Number of bytes in this piece_id %i \n",s); printf("\n\n\n Piece length for this piece_id %i \n",plength); write_piece_to_file(filenamew,piece_id, plength,phashs,myrbuf,s); write_bitfieldstatus(bitfilename,max_chunck,piece_id); } } // doing it for one piece; // n= write(sockfd,"10",2); printf("\n down download "); exit(0); } //end of while }
int contact_tracker(bt_args_t *bt_args) { printf("Please wait ...\nConnecting with tracker.\n"); char *new_file; long long leng; new_file = read_file(bt_args->torrent_file, &leng); if (!new_file) return 1; char *inf = strstr(strstr(new_file, "info"), "d"); // length on ubuntu 14.04 torrent should be 44478 long long len = be_len(inf); memset(bt_args->info_hash, '\0', BT_INFO_HASH_SIZE); memset(bt_args->bt_peer_id, '\0', BT_INFO_HASH_SIZE); SHA1((unsigned char const *) inf, (size_t) len, (unsigned char *) bt_args->info_hash); char *request_to_send = malloc(FILE_NAME_MAX); request_to_send = malloc(FILE_NAME_MAX); memset(request_to_send, '\0', FILE_NAME_MAX); memcpy(bt_args->bt_peer_id, generate_peer_id(), 20); //Port number this peer is listening on. //Common behavior is for a downloader to try to listen on //port 6881 and if that port is taken try 6882, then 6883, etc. and give up after 6889. uint16_t port = INIT_PORT; bt_args->bt_info->num_pieces = bt_args->bt_info->length / bt_args->bt_info->piece_length; sprintf(request_to_send, "%s?info_hash=%s&peer_id=%s&port=%hu&uploaded=0" "&downloaded=0&left=%d&event=started&compact=1", bt_args->bt_info->announce, url_encode(bt_args->info_hash), url_encode(bt_args->bt_peer_id), port, bt_args->bt_info->length); // correct request to send on ubuntu torrent // http://torrent.ubuntu.com:6969/announce? // info_hash=%B4%15%C9%13d%3E%5F%F4%9F%E3%7D0K%BB%5En%11%ADQ%01 // announce?info_hash=%b4%15%c9%13d%3e_%f4%9f%e3%7d0K%bb%5en%11%adQ%01 // &peer_id=TueFeb32137332015RRR&port=6681&event=started&uploaded=0 // &downloaded=0&left=1162936320&compact=1 if (bt_args->verbose) printf("Request URL for tracker: %s\n", request_to_send); char *result = _send_http_request(request_to_send); if (result) { be_node *node = be_decoden(result, (long long int) be_len); if (bt_args->verbose) be_dump(node); bt_peer *peer = malloc(sizeof(bt_peer)); // parse_info(peer, node); _fill_peer_info(peer, node, 0, ""); int num_peers = 0; char *peer_num = strstr(result, "peers"); if (peer_num == NULL) { printf("Something went wrong in parsing received data!\n"); free(result); return 1; } int i = 0; peer_num += 5; char buff[20]; memset(buff, 0, 20); for (; *peer_num != ':'; peer_num++, i++) buff[i] = *peer_num; char *endptr; num_peers = (int) strtol(buff, &endptr, 10) / 6; if (num_peers == 0) { free(result); return 1; } int count = 0; pthread_t *thread = malloc(num_peers * sizeof(pthread_t)); printf("Connecting with peers.\n"); for (i = 0; i < num_peers; i++) { uint32_t ip = *(uint32_t *) (peer->peer_hashes + count); count = (int) (count + sizeof(uint32_t)); port = *(uint16_t *) (peer->peer_hashes + count); count = (int) (count + sizeof(uint16_t)); peer_t *my_peer_t = malloc(sizeof(peer_t)); my_peer_t->interested = -1; my_peer_t->choked = -1; //IP to string struct in_addr ip_addr; ip_addr.s_addr = ip; char *id = malloc(21); memset(id, 0, 21); calc_id(inet_ntoa(ip_addr), port, id); memset(my_peer_t->id, 0, ID_SIZE); strcpy((char *) my_peer_t->id, id); init_peer(my_peer_t, id, inet_ntoa(ip_addr), htons(port)); add_peer(my_peer_t, bt_args, inet_ntoa(ip_addr), port); thdata *data = malloc(sizeof(thdata)); data->th_num = i; data->bt_args = bt_args; data->bt_peer_t = my_peer_t; pthread_create (&thread[i], NULL, (void *) &_connect_function, (void *) data); } for (i = 0; i < num_peers; i++); pthread_join(thread[i], NULL); } else { printf("Something went wrong!\n"); return 1; } return 0; }
void showbest (FILE *fp, #ifndef PCOMPLIB unsigned char **aa0, unsigned char *aa1save, int maxn, #endif struct beststr **bptr,int nbest, int qlib, struct mngmsg *m_msp, struct pstruct *ppst, struct db_str db, char **info_gstring2 #ifndef PCOMPLIB ,void **f_str #endif ) { unsigned char *aa1, *aa1a; int ntmp = 0; char bline[MAX_BLINE], fmt[40], pad[MAX_BLINE], rline[40]; char l_name[128]; int istart = 0, istop, ib; int nshow; int quiet; int r_margin; struct beststr *bbp; int n1tot; char *bp; char rel_label[12]; char tmp_str[20], *seq_code, *ann_code; int seq_code_len, ann_code_len; long loffset; /* loffset is offset from beginning of real sequence */ long l_off; /* l_off is the the virtual coordinate of residue 1 */ int n0, n1; struct rstruct rst; int lc, seqc_max, annc_max, nident, ngap; float percent, gpercent; struct a_struct *aln_p; int *tres; int gi_num; char html_pre_E[120], html_post_E[120]; #ifndef PCOMPLIB struct lmf_str *m_fptr; #endif strncpy(rel_label,"\0",2); #ifdef SHOWREL strncpy(rel_label," related",sizeof(rel_label)); #endif #ifdef SHOWUN strncpy(rel_label," unrelated",sizeof(rel_label)); #endif rel_label[sizeof(rel_label)-1]='\0'; #ifdef PCOMPLIB quiet = 1; #else quiet = m_msp->quiet; #endif n0 = m_msp->n0; if (m_msp->aln.llen > MAX_BLINE) m_msp->aln.llen = MAX_BLINE; if (ppst->zsflag < 0) r_margin = 10; else if (ppst->zsflag>=0 && m_msp->srelv > 1 ) r_margin = 19; else r_margin = 10; if (m_msp->markx & MX_M9SUMM && m_msp->show_code == SHOW_CODE_ID) { #ifdef SHOWSIM r_margin += 15; #else r_margin += 10; #endif } if (m_msp->markx & MX_HTML) { strncpy(html_pre_E,"<font color=\"darkred\">",sizeof(html_pre_E)); strncpy(html_post_E,"</font>",sizeof(html_post_E)); } else { html_pre_E[0] = html_post_E[0] = '\0'; } if (m_msp->nframe < 0) { #ifndef SUPERFAMNUM sprintf(fmt,"%%-%ds (%%4d)",m_msp->aln.llen-r_margin); #else sprintf(fmt,"%%-%ds [%%4d](%%4d)",m_msp->aln.llen-(r_margin+4)); #endif } else { sprintf(fmt,"%%-%ds (%%4d)",m_msp->aln.llen-(r_margin+4)); } memset(pad,' ',m_msp->aln.llen-(r_margin+6)); pad[m_msp->aln.llen-(r_margin+12)]='\0'; if (quiet != -1) { /* quiet is set to -1 in comp_lib2.c to force all significant hits to be shown */ nshow = 20; if (m_msp->mshow == -1) {nshow = nbest;} /* show all */ /* show specified number */ else if (m_msp->mshow_flg) { nshow = min (m_msp->mshow, nshow); } } else nshow = m_msp->nshow; if (quiet==0) istop = 20; else istop = nshow; if (quiet==0) { printf(" How many scores would you like to see? [%d] ",m_msp->nshow); fflush(stdout); if (fgets(rline,20,stdin)==NULL) exit(0); nshow = m_msp->nshow; if (rline[0]!='\n' && rline[0]!=0) sscanf(rline,"%d",&nshow); if (nshow<=0) nshow = min(20,nbest); } if ((bp = strchr (m_msp->qtitle, '\n')) != NULL) *bp = '\0'; /* fprintf (fp, "%3d %s\n", qlib,m_msp->qtitle); */ if (m_msp->markx & MX_HTML) fprintf(fp,"<p><tt><pre>\n"); if (ppst->zsflag >= 0) { if (bptr[0]->rst.escore < m_msp->e_cut) { if (m_msp->z_bits==1) {/* show bit score */ fprintf(fp,"\nThe best%s scores are:%s%s bits %sE(%ld)%s", rel_label,pad,m_msp->label,html_pre_E,ppst->zdb_size,html_post_E); } else {/* show z-score */ fprintf(fp,"\nThe best%s scores are:%s%s z-sc %sE(%ld)%s", rel_label,pad,m_msp->label,html_pre_E,ppst->zdb_size,html_post_E); } header_aux(fp); if (m_msp->markx & MX_M9SUMM) { if (m_msp->show_code == SHOW_CODE_ID) { #ifdef SHOWSIM fprintf(fp," %%_id %%_sim alen"); #else fprintf(fp," %%_id alen"); #endif } else { if (m_msp->markx & MX_HTML && m_msp->show_code !=1) { fprintf(fp,"<!-- ");} #ifndef SHOWSIM fprintf(fp,"\t%%_id %%_gid %4s alen an0 ax0 pn0 px0 an1 ax1 pn1 px1 gapq gapl fs ",m_msp->f_id1); #else fprintf(fp,"\t%%_id %%_sim %4s alen an0 ax0 pn0 px0 an1 ax1 pn1 px1 gapq gapl fs ",m_msp->f_id1); #endif } if (m_msp->show_code == SHOW_CODE_ALIGN) { fprintf(fp," aln_code"); } if (m_msp->markx & MX_HTML && m_msp->show_code!=1) { fprintf(fp," -->");} } fprintf(fp,"\n"); } else { fprintf(fp,"!! No library sequences with E() < %.2g\n",m_msp->e_cut); m_msp->nshow = 0; if (m_msp->markx & MX_HTML) fprintf(fp,"<p></tt></pre>\n"); return; } } else { fprintf(fp,"\nThe best%s scores are:%s%s",rel_label,pad,m_msp->label); header_aux(fp); if (m_msp->markx & MX_M9SUMM) { if (m_msp->show_code == SHOW_CODE_ID) { #ifdef SHOWSIM fprintf(fp," %%_id %%_sm alen"); #else fprintf(fp," %%_id alen"); #endif } else { #ifndef SHOWSIM fprintf(fp,"\t%%_id %%_gid %4s alen an0 ax0 pn0 px0 an1 ax1 pn1 px1 gapq gapl fs ",m_msp->f_id1); #else fprintf(fp,"\t%%_id %%_sim %4s alen an0 ax0 pn0 px0 an1 ax1 pn1 px1 gapq gapl fs ",m_msp->f_id1); #endif /* SHOWSIM */ } } if (m_msp->show_code == SHOW_CODE_ALIGN) { fprintf(fp," aln_code"); } fprintf(fp,"\n"); } istart = 0; l1: istop = min(nbest,nshow); for (ib=istart; ib<istop; ib++) { bbp = bptr[ib]; #ifndef PCOMPLIB #ifdef DEBUG if (bbp->seq->n1 != bbp->n1 ) { fprintf(stderr, " *** lib len error [%d!=%d] *** %s score %d\n", bbp->seq->n1,bbp->n1, bbp->seq->libstr, bbp->rst.score[0]); } #endif #endif #ifdef SUPERFAMNUM if (BBP_INFO(nsfnum) > 0 && sfn_cmp(m_msp->qsfnum_n,BBP_INFO(sfnum))) continue; #ifdef SHOWUN if (BBP_INFO(nsfnum) > 0 && sfn_cmp(m_msp->qsfnum,BBP_INFO(sfnum))) { istop = min(istop+1,nbest); /* fprintf(stderr,"skipping %d: %d==%d\n",ib,m_msp->qsfnum,BBP_INFO(sfnum)); */ continue; } #endif /* SHOWUN */ #ifdef SHOWREL if (BBP_INFO(nsfnum) == 0 || (BBP_INFO(nsfnum) > 0 && !sfn_cmp(m_msp->qsfnum,BBP_INFO(sfnum)))) { istop = min(istop+1,nbest); continue; } #endif /* SHOWREL */ #endif /* SUPERFAMNUM */ if (quiet==1 && ppst->zsflag>=0) { if (bbp->rst.escore > m_msp->e_cut) { nshow = ib; goto done; } else if (bbp->rst.escore < m_msp->e_low) continue; } #ifndef PCOMPLIB if ((m_fptr=re_openlib(bbp->seq->m_file_p,!m_msp->quiet))==NULL) { fprintf(stderr,"*** cannot re-open %s\n",bbp->seq->m_file_p->lb_name); exit(1); } RANLIB(bline,m_msp->aln.llen,bbp->seq->lseek,bbp->seq->libstr,m_fptr); #else /* PCOMPLIB */ strncpy(bline,BBP_INFO(bline),m_msp->aln.llen-r_margin); bline[m_msp->aln.llen]='\0'; #endif /* l_name is used to build an HTML link from the bestscore line to the alignment. It can also be used to discriminate multiple hits from the same long sequence. This requires that fast_pan use -m 6. */ strncpy(l_name,bline,sizeof(l_name)); /* get rid of text after second "|" */ l_name[sizeof(l_name)-1]='\0'; if ((bp=strchr(l_name,' '))!=NULL) *bp=0; if ((bp=strchr(&l_name[3],'|'))!=NULL) *bp='\0'; if (m_msp->nframe > 2) sprintf(&l_name[strlen(l_name)],"_%d",bbp->frame+1); else if (m_msp->nframe > 0 && bbp->frame == 1) strncat(l_name,"_r",sizeof(l_name)); if (bbp->seq->cont-1 > 0) { sprintf(tmp_str,":%d",bbp->seq->cont-1); strncat(l_name,tmp_str,sizeof(l_name)-strlen(l_name)); } #ifndef PCOMPLIB aln_p = &(m_msp->aln); if (m_msp->stages>1 || m_msp->markx & MX_M9SUMM) { if (bbp->seq->aa1b == NULL || (m_msp->ann_flg && bbp->seq->aa1_ann==NULL)) { /* get the sequence but don't save it */ n1 = re_getlib(aa1save, m_msp->ann_flg ? &(bbp->seq->aa1_ann) : NULL, maxn,m_msp->maxt3, m_msp->l_overlap,bbp->seq->cont,m_msp->term_code, &loffset,&l_off,bbp->seq->m_file_p); aa1 = aa1save; aa1a = bbp->seq->aa1_ann; } else { n1 = bbp->seq->n1; aa1 = bbp->seq->aa1b; aa1a = bbp->seq->aa1_ann; loffset = bbp->seq->l_offset; l_off = bbp->seq->l_off; } if (! m_msp->markx & MX_M9SUMM) { do_opt (aa0[bbp->frame], m_msp->n0, aa1, n1, bbp->frame, ppst, f_str[bbp->frame], &rst); bbp->rst.score[2]=rst.score[2]; } else { if (!bbp->have_ares) { /* showbest() can be called more than once */ do_walign(aa0[bbp->frame],m_msp->n0, aa1, n1, bbp->frame, ppst, f_str[bbp->frame], &bbp->a_res, &bbp->have_ares); /* if do_walign does not provide a fresh a_res, then copy the re-used a_res to a new location */ if (bbp->have_ares && !(bbp->have_ares & 0x2)) { if ((tres = calloc(bbp->a_res.nres+1,sizeof(int)))!=NULL) { memcpy(tres,bbp->a_res.res,sizeof(int)*bbp->a_res.nres); bbp->a_res.res = tres; bbp->have_ares |= 0x2; /* set 0x2 if has local copy */ } else { bbp->have_ares = 0; /* could not allocate memory */ } } } else { pre_cons(aa1,n1,bbp->frame,f_str[bbp->frame]); } aln_func_vals(bbp->frame, aln_p); seqc_max = bbp->a_res.nres + 4*m_msp->aln.llen+4; seq_code = NULL; seq_code_len = 0; if (m_msp->show_code == SHOW_CODE_ALIGN) { seq_code=(char *)calloc(seqc_max,sizeof(char)); /* if we have an annotation string, allocate space for the encoded annotation */ if (m_msp->ann_arr[0] != '\0') { /* the annotation encoding can be considerably longer than the alignment encoding */ annc_max = 4*seqc_max; ann_code=(char *)calloc(annc_max,sizeof(char)); } else { ann_code = NULL; annc_max = 0; } if (seq_code != NULL) { calc_astruct(aln_p, &bbp->a_res); /* we need this for offset information for calc_code, but it is incomplete so we must do it again */ cal_coord(m_msp->n0,BBP_INFO(n1), m_msp->q_offset + (m_msp->q_off-1) + (m_msp->sq0off-1), loffset + (l_off-1) + (m_msp->sq1off-1), aln_p); lc=calc_code(aa0[bbp->frame],m_msp->n0, aa1,n1, &m_msp->aln,&bbp->a_res, ppst,seq_code,seqc_max, m_msp->ann_arr, m_msp->aa0a, aa1a, ann_code, annc_max, f_str[bbp->frame]); seq_code_len = strlen(seq_code); if (ann_code != NULL) ann_code_len = strlen(ann_code); else ann_code_len = 0; } } else { lc=calc_id(aa0[bbp->frame],m_msp->n0,aa1,n1, &m_msp->aln, &bbp->a_res, ppst,f_str[bbp->frame]); } m_msp->aln.a_len = lc; nident = m_msp->aln.nident; if (lc > 0) percent = (100.0*(float)nident)/(float)lc; else percent = -1.00; ngap = m_msp->aln.ngap_q + m_msp->aln.ngap_l; #ifndef SHOWSIM if (lc-ngap > 0) gpercent = (100.0*(float)nident)/(float)(lc-ngap); else gpercent = -1.00; #else if (lc-ngap > 0) gpercent = (100.0*(float)m_msp->aln.nsim)/(float)(lc); else gpercent = -1.00; #endif /* SHOWSIM */ } } #endif /* PCOMPLIB */ n1tot = (BBP_INFO(n1tot_p)) ? *BBP_INFO(n1tot_p) : BBP_INFO(n1); bp = bline; if ((m_msp->markx & MX_HTML) && !strncmp(bline,"gi|",3)) { bp = strchr(bline+4,'|')+1; *(bp-1) = 0; gi_num = atoi(bline+3); } #ifndef SUPERFAMNUM bp[m_msp->aln.llen-r_margin]='\0'; #else bp[m_msp->aln.llen-r_margin-5]='\0'; #endif if (m_msp->nframe == -1) bp[m_msp->aln.llen-r_margin]='\0'; else bp[m_msp->aln.llen-(r_margin+4)]='\0'; #ifndef SUPERFAMNUM fprintf (fp, fmt,bp,n1tot); #else if (m_msp->nframe == -1) { fprintf (fp, fmt,bp,BBP_INFO(sfnum[0]),n1tot); } else {fprintf (fp, fmt,bp,n1tot);} #endif if (m_msp->nframe > 2) fprintf (fp, " [%d]", bbp->frame+1); else if (m_msp->nframe >= 0) fprintf(fp," [%c]",(bbp->frame > 0 ?'r':'f')); if (m_msp->srelv == 1) fprintf (fp, " %4d", bbp->rst.score[ppst->score_ix]); else { if (m_msp->srelv-1 > 0) fprintf (fp, " %4d", bbp->rst.score[0]); if (m_msp->srelv-1 > 1 || m_msp->stages>1) fprintf (fp, " %4d", bbp->rst.score[1]); fprintf (fp, " %4d", bbp->rst.score[ppst->score_ix]); } if (ppst->zsflag>=0) { if (m_msp->z_bits==1) { fprintf (fp, " %.1f %s%7.2g%s",zs_to_bit(bbp->zscore,m_msp->n0,BBP_INFO(n1)),html_pre_E,bbp->rst.escore,html_post_E); } else fprintf (fp, " %.1f %s%7.2g%s",bbp->zscore,html_pre_E,bbp->rst.escore,html_post_E); } show_aux(fp,bbp); #ifdef PCOMPLIB n1 = BBP_INFO(n1); percent = bbp->percent; gpercent = bbp->gpercent; aln_p = &(bbp->aln_d); seq_code = bbp->aln_code; seq_code_len = bbp->aln_code_n; ann_code = bbp->ann_code; ann_code_len = bbp->ann_code_n; loffset = bbp->seq->l_offset; l_off = 0; #endif if (m_msp->markx & MX_M9SUMM) { if (m_msp->show_code != SHOW_CODE_ID) { /* we need the coordinates for annotated SHOW_CODE_ALIGN */ cal_coord(m_msp->n0,BBP_INFO(n1), m_msp->q_offset + (m_msp->q_off-1) + (m_msp->sq0off-1), loffset + (l_off-1) + (m_msp->sq1off-1), aln_p); if (m_msp->markx & MX_HTML) fprintf(fp,"<!-- "); /* %_id %_sim s-w alen an0 ax0 pn0 px0 an1 ax1 pn1 px1 gapq gapl fs */ /* alignment min max min max */ /* sequence coordinate min max min max */ fprintf(fp,"\t%5.3f %5.3f %4d %4d %4ld %4ld %4ld %4ld %4ld %4ld %4ld %4ld %3d %3d %3d", percent/100.0,gpercent/100.0, #ifndef PCOMPLIB bbp->a_res.sw_score, #else bbp->sw_score, #endif aln_p->a_len, aln_p->d_start0,aln_p->d_stop0, aln_p->q_offset+1, aln_p->q_offset+m_msp->n0, aln_p->d_start1,aln_p->d_stop1, aln_p->l_offset+1, aln_p->l_offset+BBP_INFO(n1), aln_p->ngap_q,aln_p->ngap_l,aln_p->nfs); if (m_msp->show_code == SHOW_CODE_ALIGN && seq_code_len > 0 && seq_code != NULL) { fprintf(fp,"\t%s",seq_code); if (ann_code_len > 0 && ann_code != NULL) { fprintf(fp,"\t%s",ann_code); } /* fprintf(fp," [%2d:%d]",bbp->wrkr,bbp->seqnm); */ /* if we are doing MX_M10FORM and -m 9c, then we want to keep the alignment code string for the alignment output - otherwise, we can free() it If PCOMPLIB, then it is stored in bbp->ann_code*, otherwise, it's in a_res. */ #ifndef PCOMPLIB if (m_msp->markx & MX_M10FORM) { /* save encoded alignments in a_res */ if ((bbp->a_res.aln_code = (char *)calloc(seq_code_len+1,sizeof(char)))!=NULL) { strncpy(bbp->a_res.aln_code,seq_code,seq_code_len+1); bbp->a_res.aln_code[seq_code_len] = '\0'; bbp->a_res.aln_code_n = seq_code_len; } } /* always free the originally allocated encoding */ free(seq_code); seq_code = NULL; seq_code_len = 0; #else /* only free it if not to be used */ if (!(m_msp->markx & MX_M10FORM)) { free(bbp->aln_code); bbp->aln_code_n = 0; } #endif /* also clean up ann_code(_n) */ if (ann_code_len > 0 && ann_code != NULL) { #ifndef PCOMPLIB if (m_msp->markx & MX_M10FORM) { /* save encoded annotations in a_res */ if ((bbp->a_res.ann_code = (char *)calloc(ann_code_len+1,sizeof(char)))!=NULL) { strncpy(bbp->a_res.ann_code,ann_code,ann_code_len+1); bbp->a_res.ann_code[ann_code_len] = '\0'; bbp->a_res.ann_code_n = ann_code_len; } else { bbp->a_res.ann_code = NULL; bbp->a_res.ann_code_n = 0; } } free(ann_code); ann_code = NULL; ann_code_len = 0; #else if (!(m_msp->markx & MX_M10FORM)) { free(bbp->ann_code); bbp->ann_code_n = 0; } #endif } } if (m_msp->markx & MX_HTML) fprintf(fp," -->"); } else { #ifdef SHOWSIM fprintf(fp," %5.3f %5.3f %4d", percent/100.0,(float)aln_p->nsim/(float)aln_p->a_len,aln_p->a_len); #else fprintf(fp," %5.3f %4d", percent/100.0,aln_p->a_len); #endif } } if (m_msp->markx & MX_HTML) fprintf(fp," <A HREF=\"#%s\">align</A>",l_name); fprintf (fp, "\n"); fflush(fp); } if (quiet==0) { printf(" More scores? [0] "); fflush(stdout); if (fgets(rline,20,stdin)==NULL) exit(0); ntmp = 0; if (rline[0]!='\n' && rline[0]!=0) sscanf(rline,"%d",&ntmp); if (ntmp<=0) ntmp = 0; if (ntmp>0) { istart = istop; nshow += ntmp; goto l1; } } else if (quiet == 1) if (ib < nbest && (ppst->zsflag>=0 && bbp->rst.escore < m_msp->e_cut)) { if (m_msp->mshow_flg && istop >= m_msp->mshow) goto done; istart=istop; nshow += 10; goto l1; } done: m_msp->nshow = nshow; if (m_msp->markx & MX_HTML) fprintf(fp,"</pre></tt><p><hr><p>\n"); if (fp!=stdout) fprintf(fp,"\n"); }
struct a_res_str * build_ares_code(unsigned char *aa0, int n0, unsigned char *aa1, struct seq_record *seq, int frame, int *have_ares, int repeat_thresh, const struct mngmsg *m_msp, struct pstruct *ppst, void *f_str ) { unsigned char *aa1_ann; int n1; struct rstruct rst; struct a_res_str *my_ares_p, *cur_ares_p; struct a_struct *aln_p; long loffset; /* loffset is offset from beginning of real sequence */ long l_off; /* l_off is the the virtual coordinate of residue 1 */ int seqc_max, annc_max; char *seq_code, *ann_code; int seq_code_len, ann_code_len; n1 = seq->n1; aa1_ann = seq->aa1_ann; loffset = seq->l_offset; l_off = seq->l_off; if (! (*have_ares & 0x1)) { /* we don't have an a_res, and we need one */ my_ares_p = do_walign(aa0, n0, aa1, n1, frame, repeat_thresh, ppst, f_str, have_ares); } else { /* we already have the a_res */ pre_cons(aa1,n1,frame,f_str); } /* here, we need to loop through all the alignments, and produce the statistics/codes for each */ for (cur_ares_p = my_ares_p; cur_ares_p != NULL; cur_ares_p = cur_ares_p->next) { seqc_max = my_ares_p->nres + 4*m_msp->aln.llen+4; cur_ares_p->aln_code = seq_code = NULL; cur_ares_p->aln_code_n = seq_code_len = 0; cur_ares_p->ann_code = NULL; cur_ares_p->ann_code_n = 0; aln_p = &cur_ares_p->aln; /* this sets a number of constants, from the alignment function and frame, and only needs to be called once */ aln_func_vals(frame, aln_p); if ((m_msp->show_code & SHOW_CODE_ALIGN) == SHOW_CODE_ALIGN) { cur_ares_p->aln_code = seq_code=(char *)calloc(seqc_max,sizeof(char)); /* if we have an annotation string, allocate space for the encoded annotation */ if (m_msp->ann_arr[0] != '\0') { /* the annotation encoding can be considerably longer than the alignment encoding */ annc_max = 4*seqc_max; cur_ares_p->ann_code = ann_code=(char *)calloc(annc_max,sizeof(char)); } else { ann_code = NULL; annc_max = 0; } if (seq_code != NULL) { calc_astruct(aln_p, cur_ares_p); /* we need this for offset information for calc_code, but it is incomplete so we must do it again */ calc_coord(m_msp->n0,n1, m_msp->q_offset + (m_msp->q_off-1) + (m_msp->sq0off-1), loffset + (l_off-1) + (m_msp->sq1off-1), aln_p); aln_p->lc=calc_code(aa0, m_msp->n0, aa1,n1, aln_p,cur_ares_p, ppst,seq_code,seqc_max, m_msp->ann_arr, m_msp->aa0a, aa1_ann, ann_code, annc_max, f_str, m_msp->show_code); cur_ares_p->aln_code_n = seq_code_len = strlen(seq_code); if (seq_code[1] == '0' && seq_code[0] == '=') { fprintf(stderr," code begins with 0: %s\n", seq_code); } if (ann_code != NULL) ann_code_len = strlen(ann_code); else ann_code_len = 0; cur_ares_p->ann_code_n = ann_code_len; } } else { aln_p->lc=calc_id(aa0,m_msp->n0,aa1,n1, aln_p, cur_ares_p, ppst,f_str); } /* this should be all the information we need on the alignment */ } /* end for (cur_ares_p;) */ return my_ares_p; }