/* * Send the requested file. */ void xmitfile(int peer, char *port, int fd, char *name, char *mode) { struct tftphdr *rp; int n, i; uint16_t block; struct sockaddr_storage serv; /* valid server port number */ char recvbuffer[MAXPKTSIZE]; struct tftp_stats tftp_stats; stats_init(&tftp_stats); memset(&serv, 0, sizeof(serv)); rp = (struct tftphdr *)recvbuffer; if (port == NULL) { struct servent *se; se = getservbyname("tftp", "udp"); ((struct sockaddr_in *)&peer_sock)->sin_port = se->s_port; } else ((struct sockaddr_in *)&peer_sock)->sin_port = htons(atoi(port)); for (i = 0; i < 12; i++) { struct sockaddr_storage from; /* Tell the other side what we want to do */ if (debug&DEBUG_SIMPLE) printf("Sending %s\n", name); n = send_wrq(peer, name, mode); if (n > 0) { printf("Cannot send WRQ packet\n"); return; } /* * The first packet we receive has the new destination port * we have to send the next packets to. */ n = receive_packet(peer, recvbuffer, MAXPKTSIZE, &from, timeoutpacket); /* We got some data! */ if (n >= 0) { ((struct sockaddr_in *)&peer_sock)->sin_port = ((struct sockaddr_in *)&from)->sin_port; break; } /* This should be retried */ if (n == RP_TIMEOUT) { printf("Try %d, didn't receive answer from remote.\n", i + 1); continue; } /* Everything else is fatal */ break; } if (i == 12) { printf("Transfer timed out.\n"); return; } if (rp->th_opcode == ERROR) { printf("Got ERROR, aborted\n"); return; } /* * If the first packet is an OACK instead of an ACK packet, * handle it different. */ if (rp->th_opcode == OACK) { if (!options_rfc_enabled) { printf("Got OACK while options are not enabled!\n"); send_error(peer, EBADOP); return; } parse_options(peer, rp->th_stuff, n + 2); } if (read_init(fd, NULL, mode) < 0) { warn("read_init()"); return; } block = 1; tftp_send(peer, &block, &tftp_stats); read_close(); if (tftp_stats.amount > 0) printstats("Sent", verbose, &tftp_stats); txrx_error = 1; }
sendRequestedFile(char* serverIP,char* portNo, char* fName){ int sockfd, newsockfd, portno,iofd; int maxfd,connfd,maxi=-1; int i,nready; struct sockaddr_in serv_addr,server_addr; int serv_len=sizeof(serv_addr); socklen_t server_len =sizeof(server_addr); int n; char filename[128]; sockfd = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); __android_log_write(ANDROID_LOG_INFO, "tftp-client", "sendRequestedFile entered"); if (sockfd < 0) { __android_log_write(ANDROID_LOG_INFO, "tftp-client", "error opening socket"); server_clean(serverdata); } strcpy(serverdata.filename,fName); sprintf(filename,"filename is %s",serverdata.filename); __android_log_write(ANDROID_LOG_INFO, "tftp-client",filename); memset((char *) &serv_addr,0, sizeof(serv_addr)); memset((char *) &server_addr,0, sizeof(server_addr)); portno = atoi(portNo); serv_addr.sin_family = AF_INET; inet_pton(AF_INET,serverIP,&serv_addr.sin_addr.s_addr); serv_addr.sin_port = htons(portno); maxfd=sockfd; FD_ZERO(&allset); FD_SET(sockfd, &allset); printf("++++++++++++++++++++++++++Client starts+++++++++++++++++++++++++++\n"); serverdata.f=FileOpen(serverdata.filename); if(serverdata.f==NULL) { __android_log_write(ANDROID_LOG_INFO, "tftp-client", "File does not exist"); //send_error(k); //clients[k].state=2;//state change to 2 server_clean(serverdata); exit(1); } else { char filesize[128]; __android_log_write(ANDROID_LOG_INFO, "tftp-client", "File found"); fseek (serverdata.f , 0 , SEEK_END); serverdata.filesize = ftell (serverdata.f); rewind (serverdata.f); sprintf(filesize,"filesize is %d",serverdata.filesize); __android_log_write(ANDROID_LOG_INFO, "tftp-client",filesize); //printf("filesize=%d\n",serverdata.filesize); //clients[k].state=1;//state change to 1 } /*if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) { __android_log_write(ANDROID_LOG_INFO, "tftp-client", "Error connecting"); exit(1); }*/ struct timeval select_timeout; select_timeout.tv_sec=4; select_timeout.tv_usec=0; serverdata.fd=sockfd; serverdata.addr = serv_addr; serverdata.block = 1; send_wrq(); //rc4_state_clean(&rc4state); while(1){ nready=select(maxfd+1,&allset,NULL,NULL,NULL);//select return 0 if timeout ,otherwise return # of ready fd if(nready==0){//timeout char blocknum[128]; __android_log_write(ANDROID_LOG_INFO, "tftp-client", "Timeout happened"); sprintf(blocknum,"Block num last sent is %d",serverdata.block); __android_log_write(ANDROID_LOG_INFO, "tftp-client",blocknum); retransmit(); select_timeout.tv_sec=4; select_timeout.tv_usec=0; continue; } //if(FD_ISSET(sockfd,&allset)){ //new connection if(n=recvfrom(sockfd, buffer, BUFFERSIZE+4, 0,(struct sockaddr *)&server_addr,(socklen_t *)&server_len)<0) __android_log_write(ANDROID_LOG_INFO, "tftp-client","receive error"); else { rc4_init(&rc4state,rc4key,strlen(rc4key)); __android_log_write(ANDROID_LOG_INFO, "tftp-client","received some data"); serverdata.fd=sockfd; serverdata.size=0; serverdata.addr=server_addr; serverdata.addr_len=server_len; memset(serverdata.buffer,0,BUFFERSIZE+4); memset(serverdata.encryptedbuffer,0,BUFFERSIZE+4); memcpy(serverdata.encryptedbuffer,buffer,sizeof(buffer)); rc4_crypt(&rc4state,serverdata.encryptedbuffer,serverdata.buffer,sizeof(serverdata.encryptedbuffer)); //memcpy(serverdata.buffer,buffer,BUFFERSIZE+4); //rc4_crypt(serverdata.rc4state,serverdata.encryptedbuffer,serverdata.buffer,strlen(serverdata.encryptedbuffer)); state_machine(); } if(serverdata.last_block == 1) break; } serverdata.last_block =0; printf("last block sent\n"); /*for(i=0;i<MAX_CLIENT;++i){ if(clients[i].fd<0){ clients[i].fd=connfd; clients[i].size=0; clients[i].addr=cli_addr; clients[i].addr_len=cli_len; memcpy(clients[i].buffer,buffer,BUFFERSIZE); state_machine(i); clients[i].time=(int)time(NULL); break; } } if(i==MAX_CLIENT){ printf("too many clients\n"); continue; } FD_SET(connfd,&allset); if(connfd>maxfd){ maxfd=connfd; } if(i>maxi) maxi=i; if(--nready<=0){ continue; }*/ //} //old connection /*for(i=0;i<=maxi;++i){ if((iofd=clients[i].fd)<0) continue; if(FD_ISSET(iofd,&rset)){ printf("\n\n\n~~~~~~~~~~~~~~~~~~~~~~~~~OLD CONNECTION %d~~~~~~~~~~~~~~~~~~~~~~~\n",i); if(n=recvfrom(iofd, buffer, BUFFERSIZE, 0, (struct sockaddr *)&cli_addr, (socklen_t *)&cli_len)<0) error("receive error"); memcpy(clients[i].buffer,buffer,BUFFERSIZE); state_machine(i); // client_read(i); if(--nready<=0) break;/ break; } } select_timeout.tv_sec=1; select_timeout.tv_usec=0; //otherwise, timeout*/ //} }