int tftp_process_read(struct udp_pcb *upcb, struct ip_addr *to, int to_port, char* FileName) { tftp_connection_args *args = NULL; /* If Could not open the file which will be transmitted */ if (file_fopen(&file_SD, &efs1.myFs, FileName, 'r') != 0) { tftp_send_error_message(upcb, to, to_port, TFTP_ERR_FILE_NOT_FOUND); tftp_cleanup_rd(upcb, args); return 0; } /* This function is called from a callback, * therefore, interrupts are disabled, * therefore, we can use regular malloc. */ args = mem_malloc(sizeof *args); /* If we aren't able to allocate memory for a "tftp_connection_args" */ if (!args) { /* unable to allocate memory for tftp args */ tftp_send_error_message(upcb, to, to_port, TFTP_ERR_NOTDEFINED); /* no need to use tftp_cleanup_rd because no "tftp_connection_args" struct has been malloc'd */ tftp_cleanup_rd(upcb, args); return 0; } /* initialize connection structure */ args->op = TFTP_RRQ; args->to_ip.addr = to->addr; args->to_port = to_port; args->block = 1; /* block number starts at 1 (not 0) according to RFC1350 */ args->tot_bytes = 0; /* set callback for receives on this UDP PCB (Protocol Control Block) */ udp_recv(upcb, rrq_recv_callback, args); /* initiate the transaction by sending the first block of data * further blocks will be sent when ACKs are received * - the receive callbacks need to get the proper state */ tftp_send_next_block(upcb, args, to, to_port); return 1; }
/** * @brief processes tftp read operation * @param upcb: pointer on udp pcb * @param to: pointer on remote IP address * @param to_port: pointer on remote udp port * @param FileName: pointer on filename to be read * @retval error code */ int tftp_process_read(struct udp_pcb *upcb, const ip_addr_t *to, unsigned short to_port, char* FileName) { tftp_connection_args *args = NULL; char *path = malloc(strlen(FileName) + sizeof(TFTPD_PATH) + 1); if(!path) return 0; strcpy(path, TFTPD_PATH); strcat(path, FileName); /* If Could not open the file which will be transmitted */ if (f_open(&file_SD, (const TCHAR*)path, FA_READ) != FR_OK) { tftp_send_error_message(upcb, to, to_port, TFTP_ERR_FILE_NOT_FOUND); tftp_cleanup_rd(upcb, args); return 0; } args = mem_malloc(sizeof *args); /* If we aren't able to allocate memory for a "tftp_connection_args" */ if (!args) { /* unable to allocate memory for tftp args */ tftp_send_error_message(upcb, to, to_port, TFTP_ERR_NOTDEFINED); /* no need to use tftp_cleanup_rd because no "tftp_connection_args" struct has been malloc'd */ tftp_cleanup_rd(upcb, args); return 0; } /* initialize connection structure */ args->op = TFTP_RRQ; args->to_ip.addr = to->addr; args->to_port = to_port; args->block = 1; /* block number starts at 1 (not 0) according to RFC1350 */ args->tot_bytes = 0; /* set callback for receives on this UDP PCB */ udp_recv(upcb, rrq_recv_callback, args); /* initiate the transaction by sending the first block of data, further blocks will be sent when ACKs are received */ tftp_send_next_block(upcb, args, to, to_port); return 1; }
/** * @brief receive callback during tftp read operation (not on standard port 69) * @param arg: pointer on argument passed to callback * @param udp_pcb: pointer on the udp pcb * @param pkt_buf: pointer on the received pbuf * @param addr: pointer on remote IP address * @param port: pointer on remote udp port * @retval None */ void rrq_recv_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p, struct ip_addr *addr, u16_t port) { /* Get our connection state */ tftp_connection_args *args = (tftp_connection_args *)arg; if (tftp_is_correct_ack(p->payload, args->block)) { /* increment block # */ args->block++; } else { /* we did not receive the expected ACK, so do not update block #. This causes the current block to be resent. */ } /* if the last read returned less than the requested number of bytes * (i.e. TFTP_DATA_LEN_MAX), then we've sent the whole file and we can quit */ if (args->data_len < TFTP_DATA_LEN_MAX) { /* Clean the connection*/ tftp_cleanup_rd(upcb, args); pbuf_free(p); } /* if the whole file has not yet been sent then continue */ tftp_send_next_block(upcb, args, addr, port); pbuf_free(p); }