1 /****************************************************************//**
2  *
3  * @file tftp_common.c
4  *
5  * @author   Logan Gunthorpe <logang@deltatee.com>
6  *           Dirk Ziegelmeier <dziegel@gmx.de>
7  *
8  * @brief    Trivial File Transfer Protocol (RFC 1350)
9  *
10  * Copyright (c) Deltatee Enterprises Ltd. 2013
11  * All rights reserved.
12  *
13  ********************************************************************/
14 
15 /*
16  * Redistribution and use in source and binary forms, with or without
17  * modification,are permitted provided that the following conditions are met:
18  *
19  * 1. Redistributions of source code must retain the above copyright notice,
20  *    this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright notice,
22  *    this list of conditions and the following disclaimer in the documentation
23  *    and/or other materials provided with the distribution.
24  * 3. The name of the author may not be used to endorse or promote products
25  *    derived from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
28  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
29  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
30  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
32  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  *
38  * Author: Logan Gunthorpe <logang@deltatee.com>
39  *         Dirk Ziegelmeier <dziegel@gmx.de>
40  *
41  */
42 
43 /**
44  * @defgroup tftp TFTP server
45  * @ingroup apps
46  *
47  * This is simple TFTP server for the lwIP raw API.
48  */
49 
50 #if AOS_COMP_CLI
51 #include "lwip/opt.h"
52 #include "lwip/udp.h"
53 #include "lwip/timeouts.h"
54 #include "lwip/debug.h"
55 #include "lwip/apps/tftp.h"
56 #include <limits.h>
57 #include <string.h>
58 #include <aos/vfs.h>
59 #include <aos/cli.h>
60 
61 void
tftp_send_error(struct udp_pcb * pcb,const ip_addr_t * addr,u16_t port,tftp_error_t code,const char * str)62 tftp_send_error(struct udp_pcb *pcb, const ip_addr_t *addr, u16_t port,
63                 tftp_error_t code, const char *str)
64 {
65   int str_length = strlen(str);
66   struct pbuf* p;
67   u16_t* payload;
68 
69   p = pbuf_alloc(PBUF_TRANSPORT, (u16_t)(TFTP_HEADER_LENGTH + str_length + 1), PBUF_RAM);
70   if(p == NULL) {
71     return;
72   }
73 
74   payload = (u16_t*) p->payload;
75   payload[0] = PP_HTONS(TFTP_ERROR);
76   payload[1] = lwip_htons(code);
77   MEMCPY(&payload[2], str, str_length + 1);
78 
79   udp_sendto(pcb, p, addr, port);
80   pbuf_free(p);
81 }
82 
83 void
tftp_send_ack(struct udp_pcb * pcb,const ip_addr_t * addr,u16_t port,u16_t blknum)84 tftp_send_ack(struct udp_pcb *pcb, const ip_addr_t *addr, u16_t port, u16_t blknum)
85 {
86   struct pbuf* p;
87   u16_t* payload;
88 
89   p = pbuf_alloc(PBUF_TRANSPORT, TFTP_HEADER_LENGTH, PBUF_RAM);
90   if(p == NULL) {
91     return;
92   }
93   payload = (u16_t*) p->payload;
94 
95   payload[0] = PP_HTONS(TFTP_ACK);
96   payload[1] = lwip_htons(blknum);
97   udp_sendto(pcb, p, addr, port);
98   pbuf_free(p);
99 }
100 
tftp_get_done(int error,int len)101 static void tftp_get_done(int error, int len)
102 {
103     if (error == 0) {
104         aos_cli_printf("tftp received len:%d done.\r\n", len);
105     } else {
106         aos_cli_printf("tftp received failed.\r\n");
107     }
108     char _buf[PATH_MAX] = {0};
109     aos_cli_printf("(%s)#", aos_getcwd(_buf, sizeof(_buf)));
110 }
111 
112 extern tftp_context_t client_ctx;
tftp_cmd(int argc,char ** argv)113 static int tftp_cmd(int argc, char **argv)
114 {
115     if (argc < 3) {
116         goto tftp_print_usage;
117     }
118 
119     if (strncmp(argv[1], "server", 6) == 0) {
120         if (strncmp(argv[2], "start", 5) == 0) {
121             err_t err = tftp_server_start();
122             aos_cli_printf("tftp start server %s\r\n", err == ERR_OK ? "done" : "failed");
123             return 0;
124         } else if (strncmp(argv[2], "stop", 4) == 0) {
125             tftp_server_stop();
126             aos_cli_printf("tftp stop server done\r\n");
127             return 0;
128         }
129         goto tftp_print_usage;
130     } else if (strncmp(argv[1], "get", 3) == 0) {
131         ip_addr_t dst_addr;
132         uint16_t port;
133         uint8_t  binary_mode = 0;
134 
135         ipaddr_aton(argc >= 6 ? argv[2] : "10.0.0.2", &dst_addr);
136         port = (uint16_t)atoi(argv[3]);
137         tftp_client_set_server_port(port);
138 
139         if (argc == 7 &&
140             strncmp(argv[6], "binary", 6) == 0) {
141             binary_mode = 1;
142 
143         }
144 
145         tftp_client_set_binary_mode(binary_mode);
146 
147         tftp_client_get(&dst_addr, argv[4], argv[5], &client_ctx, tftp_get_done);
148         return 0;
149     }
150 
151 tftp_print_usage:
152     aos_cli_printf("usage:\r\n"
153                "  tftp server <start|stop>\r\n"
154                "  tftp get $server_ip server_src_path device_dest_path server_port file_type\r\n"
155                "eg:\r\n"
156                "  1. get file from server:\r\n"
157                "     tftp get 192.168.0.100 6068 test.txt /tmp/test.txt text\r\n"
158                "     tftp get 192.168.0.100 6068 test.zip /tmp/test.zip binary\r\n");
159     return 0;
160 }
161 
162 /* reg args: fun, cmd, description*/
163 ALIOS_CLI_CMD_REGISTER(tftp_cmd, tftp, TFTP command)
164 #endif
165 
166