1 /****************************************************************//**
2  *
3  * @file tftp.h
4  *
5  * @author   Logan Gunthorpe <logang@deltatee.com>
6  *
7  * @brief    Trivial File Transfer Protocol (RFC 1350)
8  *
9  * Copyright (c) Deltatee Enterprises Ltd. 2013
10  * All rights reserved.
11  *
12  ********************************************************************/
13 
14 /*
15  * Redistribution and use in source and binary forms, with or without
16  * modification,are permitted provided that the following conditions are met:
17  *
18  * 1. Redistributions of source code must retain the above copyright notice,
19  *    this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright notice,
21  *    this list of conditions and the following disclaimer in the documentation
22  *    and/or other materials provided with the distribution.
23  * 3. The name of the author may not be used to endorse or promote products
24  *    derived from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
27  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
28  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
29  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
31  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  *
37  * Author: Logan Gunthorpe <logang@deltatee.com>
38  *
39  */
40 
41 #ifndef LWIP_HDR_APPS_TFTP_H
42 #define LWIP_HDR_APPS_TFTP_H
43 
44 #include "lwip/apps/tftp_opts.h"
45 #include "lwip/err.h"
46 #include "lwip/pbuf.h"
47 
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51 
52 /** @ingroup tftp
53  * TFTP context containing callback functions for TFTP transfers
54  */
55 
56 #define TFTP_MAX_PAYLOAD_SIZE 512
57 #define TFTP_HEADER_LENGTH    4
58 
59 #define TFTP_RRQ   1
60 #define TFTP_WRQ   2
61 #define TFTP_DATA  3
62 #define TFTP_ACK   4
63 #define TFTP_ERROR 5
64 
65 typedef enum tftp_error_s {
66   TFTP_ERROR_FILE_NOT_FOUND    = 1,
67   TFTP_ERROR_ACCESS_VIOLATION  = 2,
68   TFTP_ERROR_DISK_FULL         = 3,
69   TFTP_ERROR_ILLEGAL_OPERATION = 4,
70   TFTP_ERROR_UNKNOWN_TRFR_ID   = 5,
71   TFTP_ERROR_FILE_EXISTS       = 6,
72   TFTP_ERROR_NO_SUCH_USER      = 7
73 } tftp_error_t;
74 
75 typedef struct tftp_context_s {
76   /**
77    * Open file for read/write.
78    * @param fname Filename
79    * @param mode Mode string from TFTP RFC 1350 (netascii, octet, mail)
80    * @param write Flag indicating read (0) or write (!= 0) access
81    * @returns File handle supplied to other functions
82    */
83   void* (*open)(const char* fname, const char* mode, u8_t write);
84   /**
85    * Close file handle
86    * @param handle File handle returned by open()
87    */
88   void (*close)(void* handle);
89   /**
90    * Read from file
91    * @param handle File handle returned by open()
92    * @param buf Target buffer to copy read data to
93    * @param bytes Number of bytes to copy to buf
94    * @returns &gt;= 0: Success; &lt; 0: Error
95    */
96   int (*read)(void* handle, void* buf, int bytes);
97   /**
98    * Write to file
99    * @param handle File handle returned by open()
100    * @param pbuf PBUF adjusted such that payload pointer points
101    *             to the beginning of write data. In other words,
102    *             TFTP headers are stripped off.
103    * @returns &gt;= 0: Success; &lt; 0: Error
104    */
105   int (*write)(void* handle, struct pbuf* p);
106 } tftp_context_t;
107 
108 typedef void (*tftp_done_cb)(int err, int length);
109 
110 err_t tftp_server_start(void);
111 void  tftp_server_stop(void);
112 int   tftp_client_get(const ip_addr_t *paddr, const char *fname, const char *lfname,
113                       tftp_context_t *ctx, tftp_done_cb cb);
114 void  tftp_client_set_server_port(uint16_t port);
115 void  tftp_client_set_binary_mode(uint8_t binary_mode);
116 
117 #ifdef __cplusplus
118 }
119 #endif
120 
121 #endif /* LWIP_HDR_APPS_TFTP_H */
122