int tftpget(const char *remote, const char *local, in_addr_t addr, bool binary) { struct sockaddr_in server; /* The address of the TFTP server */ struct sockaddr_in from; /* The address the last UDP message recv'd from */ uint8_t *packet; /* Allocated memory to hold one packet */ uint16_t blockno = 0; /* The current transfer block number */ uint16_t opcode; /* Received opcode */ uint16_t rblockno; /* Received block number */ int len; /* Generic length */ int sd; /* Socket descriptor for socket I/O */ int fd; /* File descriptor for file I/O */ int retry; /* Retry counter */ int nbytesrecvd = 0; /* The number of bytes received in the packet */ int ndatabytes; /* The number of data bytes received */ int result = ERROR; /* Assume failure */ int ret; /* Generic return status */ /* Allocate the buffer to used for socket/disk I/O */ packet = (uint8_t*)zalloc(TFTP_IOBUFSIZE); if (!packet) { ndbg("packet memory allocation failure\n"); set_errno(ENOMEM); goto errout; } /* Open the file for writing */ fd = open(local, O_WRONLY|O_CREAT|O_TRUNC, 0666); if (fd < 0) { ndbg("open failed: %d\n", errno); goto errout_with_packet; } /* Initialize a UDP socket and setup the server addresss */ sd = tftp_sockinit(&server, addr); if (sd < 0) { goto errout_with_fd; } /* Then enter the transfer loop. Loop until the entire file has * been received or until an error occurs. */ do { /* Increment the TFTP block number for the next transfer */ blockno++; /* Send the next block if the file within a loop. We will * retry up to TFTP_RETRIES times before giving up on the * transfer. */ for (retry = 0; retry < TFTP_RETRIES; retry++) { /* Send the read request using the well-known port number before * receiving the first block. Each retry of the first block will * re-send the request. */ if (blockno == 1) { len = tftp_mkreqpacket(packet, TFTP_RRQ, remote, binary); server.sin_port = HTONS(CONFIG_NETUTILS_TFTP_PORT); ret = tftp_sendto(sd, packet, len, &server); if (ret != len) { goto errout_with_sd; } /* Subsequent sendto will use the port number selected by the TFTP * server in the DATA packet. Setting the server port to zero * here indicates that we have not yet received the server port number. */ server.sin_port = 0; } /* Get the next packet from the server */ nbytesrecvd = tftp_recvfrom(sd, packet, TFTP_IOBUFSIZE, &from); /* Check if anything valid was received */ if (nbytesrecvd >= 0) { /* Verify the sender address and port number */ if (server.sin_addr.s_addr != from.sin_addr.s_addr) { nvdbg("Invalid address in DATA\n"); retry--; continue; } if (server.sin_port && server.sin_port != from.sin_port) { nvdbg("Invalid port in DATA\n"); len = tftp_mkerrpacket(packet, TFTP_ERR_UNKID, TFTP_ERRST_UNKID); ret = tftp_sendto(sd, packet, len, &from); retry--; continue; } /* Parse the incoming DATA packet */ if (nbytesrecvd < TFTP_DATAHEADERSIZE) { /* Packet is not big enough to be parsed */ nvdbg("Tiny data packet ignored\n"); continue; } if (tftp_parsedatapacket(packet, &opcode, &rblockno) != OK || blockno != rblockno) { /* Opcode is not TFTP_DATA or the block number is unexpected */ nvdbg("Parse failure\n"); if (opcode > TFTP_MAXRFC1350) { len = tftp_mkerrpacket(packet, TFTP_ERR_ILLEGALOP, TFTP_ERRST_ILLEGALOP); ret = tftp_sendto(sd, packet, len, &from); } continue; } /* Replace the server port to the one in the good data response */ if (!server.sin_port) { server.sin_port = from.sin_port; } /* Then break out of the loop */ break; } } /* Did we exhaust all of the retries? */ if (retry == TFTP_RETRIES) { nvdbg("Retry limit exceeded\n"); goto errout_with_sd; } /* Write the received data chunk to the file */ ndatabytes = nbytesrecvd - TFTP_DATAHEADERSIZE; tftp_dumpbuffer("Recvd DATA", packet + TFTP_DATAHEADERSIZE, ndatabytes); if (tftp_write(fd, packet + TFTP_DATAHEADERSIZE, ndatabytes) < 0) { goto errout_with_sd; } /* Send the acknowledgment */ len = tftp_mkackpacket(packet, blockno); ret = tftp_sendto(sd, packet, len, &server); if (ret != len) { goto errout_with_sd; } nvdbg("ACK blockno %d\n", blockno); } while (ndatabytes >= TFTP_DATASIZE); /* Return success */ result = OK; errout_with_sd: close(sd); errout_with_fd: close(fd); errout_with_packet: free(packet); errout: return result; }
int tftpput(const char *local, const char *remote, in_addr_t addr, bool binary) { struct sockaddr_in server; /* The address of the TFTP server */ uint8_t *packet; /* Allocated memory to hold one packet */ off_t offset; /* Offset into source file */ uint16_t blockno; /* The current transfer block number */ uint16_t rblockno; /* The ACK'ed block number */ uint16_t port = 0; /* This is the port number for the transfer */ int packetlen; /* The length of the data packet */ int sd; /* Socket descriptor for socket I/O */ int fd; /* File descriptor for file I/O */ int retry; /* Retry counter */ int result = ERROR; /* Assume failure */ int ret; /* Generic return status */ /* Allocate the buffer to used for socket/disk I/O */ packet = (uint8_t*)zalloc(TFTP_IOBUFSIZE); if (!packet) { ndbg("packet memory allocation failure\n"); set_errno(ENOMEM); goto errout; } /* Open the file for reading */ fd = open(local, O_RDONLY); if (fd < 0) { ndbg("open failed: %d\n", errno); goto errout_with_packet; } /* Initialize a UDP socket and setup the server addresss */ sd = tftp_sockinit(&server, addr); if (sd < 0) { goto errout_with_fd; } /* Send the write request using the well known port. This may need * to be done several times because (1) UDP is inherenly unreliable * and packets may be lost normally, and (2) uIP has a nasty habit * of droppying packets if there is nothing hit in the ARP table. */ blockno = 1; retry = 0; for (;;) { packetlen = tftp_mkreqpacket(packet, TFTP_WRQ, remote, binary); ret = tftp_sendto(sd, packet, packetlen, &server); if (ret != packetlen) { goto errout_with_sd; } /* Receive the ACK for the write request */ if (tftp_rcvack(sd, packet, &server, &port, NULL) == 0) { break; } ndbg("Re-sending request\n"); /* We are going to loop and re-send the request packet. Check the * retry count so that we do not loop forever. */ if (++retry > TFTP_RETRIES) { ndbg("Retry count exceeded\n"); set_errno(ETIMEDOUT); goto errout_with_sd; } } /* Then loop sending the entire file to the server in chunks */ offset = 0; retry = 0; for (;;) { /* Construct the next data packet */ packetlen = tftp_mkdatapacket(fd, offset, packet, blockno); if (packetlen < 0) { goto errout_with_sd; } /* Send the next data chunk */ ret = tftp_sendto(sd, packet, packetlen, &server); if (ret != packetlen) { goto errout_with_sd; } /* Check for an ACK for the data chunk */ if (tftp_rcvack(sd, packet, &server, &port, &rblockno) == OK) { /* Check if the packet that we just sent was ACK'ed. If not, * we just loop to resend the same packet (same blockno, same * file offset). */ if (rblockno == blockno) { /* Yes.. If we are at the end of the file and if all of the packets * have been ACKed, then we are done. */ if (packetlen < TFTP_PACKETSIZE) { break; } /* Not the last block.. set up for the next block */ blockno++; offset += TFTP_DATASIZE; retry = 0; /* Skip the retry test */ continue; } } /* We are going to loop and re-send the data packet. Check the retry * count so that we do not loop forever. */ if (++retry > TFTP_RETRIES) { ndbg("Retry count exceeded\n"); set_errno(ETIMEDOUT); goto errout_with_sd; } } /* Return success */ result = OK; errout_with_sd: close(sd); errout_with_fd: close(fd); errout_with_packet: free(packet); errout: return result; }