tftp_example.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #include <stdio.h>
  30. #include "lwip/apps/tftp_client.h"
  31. #include "lwip/apps/tftp_server.h"
  32. #include "tftp_example.h"
  33. #include <string.h>
  34. #if LWIP_UDP
  35. /* Define a base directory for TFTP access
  36. * ATTENTION: This code does NOT check for sandboxing,
  37. * i.e. '..' in paths is not checked! */
  38. #ifndef LWIP_TFTP_EXAMPLE_BASE_DIR
  39. #define LWIP_TFTP_EXAMPLE_BASE_DIR ""
  40. #endif
  41. /* Define this to a file to get via tftp client */
  42. #ifndef LWIP_TFTP_EXAMPLE_CLIENT_FILENAME
  43. #define LWIP_TFTP_EXAMPLE_CLIENT_FILENAME "test.bin"
  44. #endif
  45. /* Define this to a server IP string */
  46. #ifndef LWIP_TFTP_EXAMPLE_CLIENT_REMOTEIP
  47. #define LWIP_TFTP_EXAMPLE_CLIENT_REMOTEIP "192.168.0.1"
  48. #endif
  49. static char full_filename[256];
  50. static void *
  51. tftp_open_file(const char* fname, u8_t is_write)
  52. {
  53. snprintf(full_filename, sizeof(full_filename), "%s%s", LWIP_TFTP_EXAMPLE_BASE_DIR, fname);
  54. full_filename[sizeof(full_filename)-1] = 0;
  55. if (is_write) {
  56. return (void*)fopen(full_filename, "wb");
  57. } else {
  58. return (void*)fopen(full_filename, "rb");
  59. }
  60. }
  61. static void*
  62. tftp_open(const char* fname, const char* mode, u8_t is_write)
  63. {
  64. LWIP_UNUSED_ARG(mode);
  65. return tftp_open_file(fname, is_write);
  66. }
  67. static void
  68. tftp_close(void* handle)
  69. {
  70. fclose((FILE*)handle);
  71. }
  72. static int
  73. tftp_read(void* handle, void* buf, int bytes)
  74. {
  75. int ret = fread(buf, 1, bytes, (FILE*)handle);
  76. if (ret <= 0) {
  77. return -1;
  78. }
  79. return ret;
  80. }
  81. static int
  82. tftp_write(void* handle, struct pbuf* p)
  83. {
  84. while (p != NULL) {
  85. if (fwrite(p->payload, 1, p->len, (FILE*)handle) != (size_t)p->len) {
  86. return -1;
  87. }
  88. p = p->next;
  89. }
  90. return 0;
  91. }
  92. /* For TFTP client only */
  93. static void
  94. tftp_error(void* handle, int err, const char* msg, int size)
  95. {
  96. char message[100];
  97. LWIP_UNUSED_ARG(handle);
  98. memset(message, 0, sizeof(message));
  99. MEMCPY(message, msg, LWIP_MIN(sizeof(message)-1, (size_t)size));
  100. printf("TFTP error: %d (%s)", err, message);
  101. }
  102. static const struct tftp_context tftp = {
  103. tftp_open,
  104. tftp_close,
  105. tftp_read,
  106. tftp_write,
  107. tftp_error
  108. };
  109. void
  110. tftp_example_init_server(void)
  111. {
  112. tftp_init_server(&tftp);
  113. }
  114. void
  115. tftp_example_init_client(void)
  116. {
  117. void *f;
  118. err_t err;
  119. ip_addr_t srv;
  120. int ret = ipaddr_aton(LWIP_TFTP_EXAMPLE_CLIENT_REMOTEIP, &srv);
  121. LWIP_ASSERT("ipaddr_aton failed", ret == 1);
  122. LWIP_UNUSED_ARG(ret);
  123. err = tftp_init_client(&tftp);
  124. LWIP_ASSERT("tftp_init_client failed", err == ERR_OK);
  125. f = tftp_open_file(LWIP_TFTP_EXAMPLE_CLIENT_FILENAME, 1);
  126. LWIP_ASSERT("failed to create file", f != NULL);
  127. err = tftp_get(f, &srv, TFTP_PORT, LWIP_TFTP_EXAMPLE_CLIENT_FILENAME, TFTP_MODE_OCTET);
  128. LWIP_ASSERT("tftp_get failed", err == ERR_OK);
  129. }
  130. #endif /* LWIP_UDP */