• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Redistribution and use in source and binary forms, with or without modification,
3  * are permitted provided that the following conditions are met:
4  *
5  * 1. Redistributions of source code must retain the above copyright notice,
6  *    this list of conditions and the following disclaimer.
7  * 2. Redistributions in binary form must reproduce the above copyright notice,
8  *    this list of conditions and the following disclaimer in the documentation
9  *    and/or other materials provided with the distribution.
10  * 3. The name of the author may not be used to endorse or promote products
11  *    derived from this software without specific prior written permission.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
14  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
15  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
16  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
17  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
18  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
19  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
20  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
21  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
22  * OF SUCH DAMAGE.
23  *
24  * This file is part of the lwIP TCP/IP stack.
25  *
26  * Author: Dirk Ziegelmeier <dziegel@gmx.de>
27  *
28  */
29 
30 #include <stdio.h>
31 
32 #include "lwip/apps/tftp_client.h"
33 #include "lwip/apps/tftp_server.h"
34 #include "tftp_example.h"
35 
36 #include <string.h>
37 
38 #if LWIP_UDP
39 
40 /* Define a base directory for TFTP access
41  * ATTENTION: This code does NOT check for sandboxing,
42  * i.e. '..' in paths is not checked! */
43 #ifndef LWIP_TFTP_EXAMPLE_BASE_DIR
44 #define LWIP_TFTP_EXAMPLE_BASE_DIR ""
45 #endif
46 
47 /* Define this to a file to get via tftp client */
48 #ifndef LWIP_TFTP_EXAMPLE_CLIENT_FILENAME
49 #define LWIP_TFTP_EXAMPLE_CLIENT_FILENAME "test.bin"
50 #endif
51 
52 /* Define this to a server IP string */
53 #ifndef LWIP_TFTP_EXAMPLE_CLIENT_REMOTEIP
54 #define LWIP_TFTP_EXAMPLE_CLIENT_REMOTEIP "192.168.0.1"
55 #endif
56 
57 static char full_filename[256];
58 
59 static void *
tftp_open_file(const char * fname,u8_t is_write)60 tftp_open_file(const char* fname, u8_t is_write)
61 {
62   snprintf(full_filename, sizeof(full_filename), "%s%s", LWIP_TFTP_EXAMPLE_BASE_DIR, fname);
63   full_filename[sizeof(full_filename)-1] = 0;
64 
65   if (is_write) {
66     return (void*)fopen(full_filename, "wb");
67   } else {
68     return (void*)fopen(full_filename, "rb");
69   }
70 }
71 
72 static void*
tftp_open(const char * fname,const char * mode,u8_t is_write)73 tftp_open(const char* fname, const char* mode, u8_t is_write)
74 {
75   LWIP_UNUSED_ARG(mode);
76   return tftp_open_file(fname, is_write);
77 }
78 
79 static void
tftp_close(void * handle)80 tftp_close(void* handle)
81 {
82   fclose((FILE*)handle);
83 }
84 
85 static int
tftp_read(void * handle,void * buf,int bytes)86 tftp_read(void* handle, void* buf, int bytes)
87 {
88   int ret = fread(buf, 1, bytes, (FILE*)handle);
89   if (ret <= 0) {
90     return -1;
91   }
92   return ret;
93 }
94 
95 static int
tftp_write(void * handle,struct pbuf * p)96 tftp_write(void* handle, struct pbuf* p)
97 {
98   while (p != NULL) {
99     if (fwrite(p->payload, 1, p->len, (FILE*)handle) != (size_t)p->len) {
100       return -1;
101     }
102     p = p->next;
103   }
104 
105   return 0;
106 }
107 
108 /* For TFTP client only */
109 static void
tftp_error(void * handle,int err,const char * msg,int size)110 tftp_error(void* handle, int err, const char* msg, int size)
111 {
112   char message[100];
113 
114   LWIP_UNUSED_ARG(handle);
115 
116   memset(message, 0, sizeof(message));
117   MEMCPY(message, msg, LWIP_MIN(sizeof(message)-1, (size_t)size));
118 
119   printf("TFTP error: %d (%s)", err, message);
120 }
121 
122 static const struct tftp_context tftp = {
123   tftp_open,
124   tftp_close,
125   tftp_read,
126   tftp_write,
127   tftp_error
128 };
129 
130 void
tftp_example_init_server(void)131 tftp_example_init_server(void)
132 {
133   tftp_init_server(&tftp);
134 }
135 
136 void
tftp_example_init_client(void)137 tftp_example_init_client(void)
138 {
139   void *f;
140   err_t err;
141   ip_addr_t srv;
142   int ret = ipaddr_aton(LWIP_TFTP_EXAMPLE_CLIENT_REMOTEIP, &srv);
143   LWIP_ASSERT("ipaddr_aton failed", ret == 1);
144   LWIP_UNUSED_ARG(ret);
145 
146   err = tftp_init_client(&tftp);
147   LWIP_ASSERT("tftp_init_client failed", err == ERR_OK);
148 
149   f = tftp_open_file(LWIP_TFTP_EXAMPLE_CLIENT_FILENAME, 1);
150   LWIP_ASSERT("failed to create file", f != NULL);
151 
152   err = tftp_get(f, &srv, TFTP_PORT, LWIP_TFTP_EXAMPLE_CLIENT_FILENAME, TFTP_MODE_OCTET);
153   LWIP_ASSERT("tftp_get failed", err == ERR_OK);
154 }
155 
156 #endif /* LWIP_UDP */
157