char * decodePort(char * m , HeapHandler h){ char * encodedPort , * q , * r; int i, found = 0; for(i=0; m[i] != '\0' ; i++){ if(m[i] == ','){ found++; if(found == 4){ i++; break; } } } encodedPort = subString(m , i , strlen(m) , h); q = subString(encodedPort , 0 , indexOfString(encodedPort , ",")-1 , h); r = subString(encodedPort , indexOfString(encodedPort , ",")+1 , strlen(encodedPort) , h); return intToString(atoi(q)*256 + atoi(r) , h); }
char* Read(char* filename){ FILE* fp = fopen(filename,"w"); printf("goes in read function\n**************************************************************************************\n\n\n"); int data_len = 0; int count = 0; u_int32_t previousSeq = 1; struct TCP_header * tcph = (struct TCP_header *) (datagram + sizeof(struct ip)); int rcvlen = 0; char* msg = NULL; u_int16_t datasize = 0; int contentlength = 50000; int first_http_pack = 1; char* http_buf = NULL; char buf[1024]; struct TCP_header* ptcph = (struct TCP_header *)(buf + sizeof(struct iphdr)); struct iphdr* iph = (struct iphdr*) buf; do{ printf("\n\n"); memset(buf,0,1024); // Receive packet if((rcvlen = recv(sock2recv,buf,1024,0)) < 0){ perror("recv failed"); exit(0); } if(recv_flag_contains(FIN,buf)) { printf("Packet signal SYN, gonna quit\n"); break; } msg = buf + sizeof(struct iphdr) + 4 * ptcph->doff; // printf("\n****************************************\n********************** msg is\n%s\n**********************************\n***************************************\n",msg); printf("recvlen is %d\tseq num is: %d\n",rcvlen,(ntohl(ptcph->seq) - relative_first_seq)); if(rcvlen == 0){ continue; }; datasize = ntohs(iph->tot_len) - (sizeof(struct iphdr) + 4 * ptcph->doff); // if data size greater than zero if(datasize > 0){ printf("datasize is %d\n",datasize); if(previousSeq >= ntohl(ptcph->seq)|| not_correct_seq(buf,ackNum)){ printf("Got duplicate packet previousSeq is\t%u now seq is \t%u\tdatasize is\t%d\n",(previousSeq - relative_first_seq),(ntohl(ptcph->seq) - relative_first_seq),datasize); retransmit(buf); continue; } msg = buf + sizeof(struct iphdr) + 4 * ptcph->doff; count = strlen(msg); // handle http header if(first_http_pack){ data_len = datasize; char* slen = NULL; // printf("\nFirst time\n"); // printf("msg is :\n%s\n",msg); first_http_pack = 0; if(!(contain_substr(msg,"HTTP/") && contain_substr(msg, "200") && contain_substr(msg,"OK")) ) { printf("HTTP header code is not \"200 OK\", gonna quit!\n"); // printf("Msg is\n%s\n",msg); exit(0); } slen = indexOfString(msg,"Content-Length"); if(slen != NULL){ slen = strchr(slen,':'); slen++; contentlength = atoi(slen); printf("Content-Length is :\t %d\n",contentlength); } else { printf("\ndid not find Content-Length\n\n"); exit(0); } printf("First time\n"); http_buf = malloc(contentlength + 1000); memset(http_buf,0,contentlength + 1000); } strcat(http_buf,msg); // printf("BODY\n%s\n",msg); previousSeq = ntohl(ptcph->seq); printf("previousSeq is %d\n",(previousSeq - relative_first_seq)); } else { if(ntohl(ackNum) > contentlength){ printf("got all data from packets: %u bytes, gonna quit\n",(ntohl(ackNum) - relative_first_seq)); break; } printf("Received rubbish, go on\n"); continue; } // Respond ACK iph->tot_len = sizeof(struct iphdr) + sizeof(struct TCP_header) ; tcph->seq = ptcph->ack_seq; tcph->ack_seq = htonl(ntohl(ptcph->seq) + datasize); tcph->psh = 0; tcph->doff = 5; tcph->ack = 1; tcph->fin = 0; tcph->rst = 0; /* Check sum */ tcph->check = 0; tcph->check = tcp_csum(send_addr,tcph,0,srcaddr); if(sendto(sock2send,datagram,iph->tot_len, 0, (struct sockaddr *)&send_addr, sizeof(send_addr)) < 0) { perror("sendto failed"); } printf("Send succss\n"); ackNum = htonl((ntohl(ackNum) + datasize)); }while((!recv_flag_contains(FIN,buf))); // printf("receive size is %d\n",count); // printf("%s\n",http_buf); printf("Read done!\n"); fwrite(http_buf,1,(contentlength + 1000),fp); return http_buf; }