[457] | 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_server.h"
|
---|
| 33 | #include "tftp_example.h"
|
---|
| 34 |
|
---|
| 35 | #if LWIP_UDP
|
---|
| 36 |
|
---|
| 37 | static void*
|
---|
| 38 | tftp_open(const char* fname, const char* mode, u8_t is_write)
|
---|
| 39 | {
|
---|
| 40 | LWIP_UNUSED_ARG(mode);
|
---|
| 41 |
|
---|
| 42 | if (is_write) {
|
---|
| 43 | return (void*)fopen(fname, "wb");
|
---|
| 44 | } else {
|
---|
| 45 | return (void*)fopen(fname, "rb");
|
---|
| 46 | }
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | static void
|
---|
| 50 | tftp_close(void* handle)
|
---|
| 51 | {
|
---|
| 52 | fclose((FILE*)handle);
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | static int
|
---|
| 56 | tftp_read(void* handle, void* buf, int bytes)
|
---|
| 57 | {
|
---|
| 58 | int ret = fread(buf, 1, bytes, (FILE*)handle);
|
---|
| 59 | if (ret <= 0) {
|
---|
| 60 | return -1;
|
---|
| 61 | }
|
---|
| 62 | return ret;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | static int
|
---|
| 66 | tftp_write(void* handle, struct pbuf* p)
|
---|
| 67 | {
|
---|
| 68 | while (p != NULL) {
|
---|
| 69 | if (fwrite(p->payload, 1, p->len, (FILE*)handle) != (size_t)p->len) {
|
---|
| 70 | return -1;
|
---|
| 71 | }
|
---|
| 72 | p = p->next;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | return 0;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | static const struct tftp_context tftp = {
|
---|
| 79 | tftp_open,
|
---|
| 80 | tftp_close,
|
---|
| 81 | tftp_read,
|
---|
| 82 | tftp_write
|
---|
| 83 | };
|
---|
| 84 |
|
---|
| 85 | void
|
---|
| 86 | tftp_example_init(void)
|
---|
| 87 | {
|
---|
| 88 | tftp_init(&tftp);
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | #endif /* LWIP_UDP */
|
---|