XINU
Data Structures | Macros | Functions
tftp.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  tftp_msg
 

Macros

#define TFTP_ACK   4 /* Acknowledgement */
 
#define TFTP_DATA   3 /* Data Packet */
 
#define TFTP_ERROR   5 /* Error */
 
#define TFTP_ERROR_ACCESS_VIOLATION   2 /* Access violation. */
 
#define TFTP_ERROR_DISK_FULL   3 /* Disk full or allocation exceeded. */
 
#define TFTP_ERROR_FILE_EXISTS   6 /* File already exists. */
 
#define TFTP_ERROR_FILE_NOT_FOUND   1 /* File not found. */
 
#define TFTP_ERROR_ILLEGAL_OP   4 /* Illegal TFTP operation. */
 
#define TFTP_ERROR_NO_SUCH_USER   7 /* No such user. */
 
#define TFTP_ERROR_NOT_DEFINED   0 /* Not defined, see error message (if any). */
 
#define TFTP_ERROR_UNKNOWN_TRANSFER_ID   5 /* Unknown transfer ID. */
 
#define TFTP_MAXDATA   512 /* Max size of a data packet */
 
#define TFTP_MAXNAM   64 /* Max length of a file name */
 
#define TFTP_MAXRETRIES   3 /* Number of retranmissions */
 
#define TFTP_NON_VERBOSE   0 /* Do not use verbose output */
 
#define TFTP_PORT   69 /* UDP Port for TFTP */
 
#define TFTP_RRQ   1 /* Read Request */
 
#define TFTP_VERBOSE   1 /* Use verbose output */
 
#define TFTP_WAIT   5000 /* Time to wait for reply (ms) */
 
#define TFTP_WRQ   2 /* Write Request */
 

Functions

status tftpget (uint32 serverip, const char *filename, char *rcv_buf, uint32 rcv_buf_size, byte verbose)
 
status tftpget_mb (uint32 serverip, const char *filename, char **rcv_bufs, uint32 *rcv_buf_sizes, uint32 rcv_buf_count, byte verbose)
 

Macro Definition Documentation

◆ TFTP_ACK

#define TFTP_ACK   4 /* Acknowledgement */

Definition at line 7 of file tftp.h.

Referenced by tftpget_mb().

◆ TFTP_DATA

#define TFTP_DATA   3 /* Data Packet */

Definition at line 6 of file tftp.h.

Referenced by tftp_send1().

◆ TFTP_ERROR

#define TFTP_ERROR   5 /* Error */

Definition at line 8 of file tftp.h.

Referenced by tftp_send1().

◆ TFTP_ERROR_ACCESS_VIOLATION

#define TFTP_ERROR_ACCESS_VIOLATION   2 /* Access violation. */

Definition at line 13 of file tftp.h.

◆ TFTP_ERROR_DISK_FULL

#define TFTP_ERROR_DISK_FULL   3 /* Disk full or allocation exceeded. */

Definition at line 14 of file tftp.h.

◆ TFTP_ERROR_FILE_EXISTS

#define TFTP_ERROR_FILE_EXISTS   6 /* File already exists. */

Definition at line 17 of file tftp.h.

◆ TFTP_ERROR_FILE_NOT_FOUND

#define TFTP_ERROR_FILE_NOT_FOUND   1 /* File not found. */

Definition at line 12 of file tftp.h.

◆ TFTP_ERROR_ILLEGAL_OP

#define TFTP_ERROR_ILLEGAL_OP   4 /* Illegal TFTP operation. */

Definition at line 15 of file tftp.h.

◆ TFTP_ERROR_NO_SUCH_USER

#define TFTP_ERROR_NO_SUCH_USER   7 /* No such user. */

Definition at line 18 of file tftp.h.

◆ TFTP_ERROR_NOT_DEFINED

#define TFTP_ERROR_NOT_DEFINED   0 /* Not defined, see error message (if any). */

Definition at line 11 of file tftp.h.

◆ TFTP_ERROR_UNKNOWN_TRANSFER_ID

#define TFTP_ERROR_UNKNOWN_TRANSFER_ID   5 /* Unknown transfer ID. */

Definition at line 16 of file tftp.h.

◆ TFTP_MAXDATA

#define TFTP_MAXDATA   512 /* Max size of a data packet */

Definition at line 22 of file tftp.h.

◆ TFTP_MAXNAM

#define TFTP_MAXNAM   64 /* Max length of a file name */

Definition at line 21 of file tftp.h.

Referenced by tftpget_mb().

◆ TFTP_MAXRETRIES

#define TFTP_MAXRETRIES   3 /* Number of retranmissions */

Definition at line 23 of file tftp.h.

Referenced by tftpget_mb().

◆ TFTP_NON_VERBOSE

#define TFTP_NON_VERBOSE   0 /* Do not use verbose output */

Definition at line 27 of file tftp.h.

◆ TFTP_PORT

#define TFTP_PORT   69 /* UDP Port for TFTP */

Definition at line 20 of file tftp.h.

Referenced by tftpget_mb().

◆ TFTP_RRQ

#define TFTP_RRQ   1 /* Read Request */

Definition at line 4 of file tftp.h.

Referenced by tftpget_mb().

◆ TFTP_VERBOSE

#define TFTP_VERBOSE   1 /* Use verbose output */

Definition at line 28 of file tftp.h.

Referenced by tftpget_mb().

◆ TFTP_WAIT

#define TFTP_WAIT   5000 /* Time to wait for reply (ms) */

Definition at line 24 of file tftp.h.

Referenced by tftp_send1().

◆ TFTP_WRQ

#define TFTP_WRQ   2 /* Write Request */

Definition at line 5 of file tftp.h.

Function Documentation

◆ tftpget()

status tftpget ( uint32  serverip,
const char *  filename,
char *  rcv_buf,
uint32  rcv_buf_size,
byte  verbose 
)

Definition at line 100 of file tftp.c.

References tftpget_mb().

107 {
108  return tftpget_mb(serverip, filename, &rcv_buf, &rcv_buf_size, 1,
109  verbose);
110 }
status tftpget_mb(uint32 serverip, const char *filename, char **rcv_bufs, uint32 *rcv_buf_sizes, uint32 rcv_buf_count, byte verbose)
Definition: tftp.c:118
Here is the call graph for this function:

◆ tftpget_mb()

status tftpget_mb ( uint32  serverip,
const char *  filename,
char **  rcv_bufs,
uint32 rcv_buf_sizes,
uint32  rcv_buf_count,
byte  verbose 
)

Definition at line 118 of file tftp.c.

References getport(), htons, kprintf(), memset(), NULL, NULLCH, strncpy(), strnlen(), SYSERR, tftp_msg::tf_data, tftp_msg::tf_dblk, tftp_msg::tf_opcode, TFTP_ACK, TFTP_MAXNAM, TFTP_MAXRETRIES, TFTP_PORT, TFTP_RRQ, tftp_send1(), TFTP_VERBOSE, TIMEOUT, udp_register(), udp_release(), and udp_sendto().

Referenced by tftpget().

126 {
127  int32 nlen; /* Length of file name */
128  uint16 localport; /* Local UDP port to use */
129  uint16 remport=TFTP_PORT; /* Remote UDP port to use */
130  int32 sock; /* Socket descriptor to use */
131  uint16 expected = 1; /* Next data block we expect */
132  int32 i; /* Loop index */
133  int32 n; /* Num,h. octets in incoming msg*/
134  int32 ret; /* Return value */
135  int32 filesiz; /* Total size of downloaded file*/
136  struct tftp_msg outmsg; /* Outgoing message */
137  int32 mlen; /* Length of outgoing mesage */
138  struct tftp_msg inmsg; /* Buffer for response message */
139  int32 dlen; /* Size of data in a response */
140  char* curr_buf; /* Current buffer being used */
141  uint32 curr_buf_ind; /* Index of current buffer */
142  uint32 curr_used; /* Amount used in buffer */
143 
144  /* Check args */
145 
146  if(filename == NULL || serverip == 0 || rcv_bufs == NULL ||
147  rcv_buf_sizes == NULL || rcv_buf_count == 0) {
148  kprintf("[TFTP GET] ERROR: Invalid argument\n");
149  return SYSERR;
150  }
151  for(i = 0; i < rcv_buf_count; i++) {
152  if(rcv_bufs[i] == NULL || rcv_buf_sizes[i] == 0) {
153  kprintf("[TFTP GET] ERROR: Invalid argument\n");
154  return SYSERR;
155  }
156  }
157 
158  nlen = strnlen(filename, TFTP_MAXNAM+1);
159  if ( (nlen <= 0) || (nlen > TFTP_MAXNAM) ) {
160  return SYSERR;
161  }
162 
163  if(verbose & TFTP_VERBOSE) {
164  kprintf("[TFTP Get] Server: %08X File: %s\n",
165  serverip, filename);
166  }
167 
168  /* Generate a local port */
169 
170  localport = getport();
171 
172  if (verbose & TFTP_VERBOSE) {
173  kprintf("[TFTP Get] Using local port %u\n",
174  0xffff & localport);
175  }
176 
177  /* Register a UDP socket */
178 
179  sock = udp_register(serverip, 0, localport);
180  if (sock == SYSERR) {
181  kprintf("[TFTP Get] ERROR: udp_register failed\n");
182  return SYSERR;
183  }
184 
185  /* Clear the message buffer */
186 
187  memset((char*)&outmsg, NULLCH, sizeof(outmsg));
188 
189  /* Initialize the total file size to zero */
190 
191  filesiz = 0;
192  curr_buf_ind = 0;
193  curr_buf = (char*)rcv_bufs[curr_buf_ind];
194  curr_used = 0;
195 
196  /* Form the first message and compute length (a Read Request) */
197 
198  outmsg.tf_opcode = htons(TFTP_RRQ);
199  strncpy(outmsg.tf_filemode, filename, nlen+1);
200  /* Set mode to 'octet' */
201  strncpy(outmsg.tf_filemode+nlen+1, "octet", sizeof("octet")+1);
202 
203  /* Length is 2 opcode octets, name length, "octet" length, */
204  /* and two NULLs */
205 
206  mlen = nlen + strnlen("octet", 6) + 4;
207 
208  /* Repeatedly send the next request and get a response, */
209  /* retransmitting a request up to TFTP_MAXRETRIES times */
210 
211  while(1) {
212  for (i=0; i < TFTP_MAXRETRIES; i++) {
213  n = tftp_send1(sock, serverip, &remport, &outmsg, mlen,
214  &inmsg, expected);
215  if (n > 0) {
216  break;
217  } else if (n == SYSERR) {
218  kprintf("\n[TFTP Get] ERROR: TFTP Send fails\n");
219  udp_release(sock);
220  return SYSERR;
221  } else if (n == TIMEOUT) {
222  continue;
223 
224  }
225  }
226  if (i >= TFTP_MAXRETRIES) {
227  kprintf("\n[TFTP Get] ERROR: Max retries %d exceeded\n",
228  TFTP_MAXRETRIES);
229  udp_release(sock);
230  return SYSERR;
231  }
232 
233  if(verbose & TFTP_VERBOSE) {
234  kprintf(".");
235  }
236 
237  /* Compute size of data in the message */
238 
239  dlen = n - sizeof(inmsg.tf_opcode) - sizeof(inmsg.tf_dblk);
240 
241  /* Move the contents of this block into the file buffer */
242 
243  for (i=0; i<dlen; i++) {
244  if (curr_used >= rcv_buf_sizes[curr_buf_ind]) {
245  curr_buf_ind++;
246  if(curr_buf_ind >= rcv_buf_count) {
247  udp_release(sock);
248  if(verbose & TFTP_VERBOSE) {
249  kprintf("\n");
250  }
251  return filesiz;
252  }
253  curr_buf = (char*)rcv_bufs[curr_buf_ind];
254  curr_used = 0;
255  }
256  *curr_buf++ = inmsg.tf_data[i];
257  curr_used++;
258  filesiz++;
259  }
260 
261  /* Form an ACK */
262 
263  outmsg.tf_opcode = htons(TFTP_ACK);
264  outmsg.tf_ablk = htons(expected);
265  mlen = sizeof(outmsg.tf_opcode) + sizeof(outmsg.tf_ablk);
266 
267  /* If this was the last packet, send final ACK */
268 
269  if (dlen < 512) {
270  ret = udp_sendto(sock, serverip, remport,
271  (char *) &outmsg, mlen);
272  udp_release(sock);
273 
274  if(verbose & TFTP_VERBOSE) {
275  kprintf("\n");
276  }
277 
278  if (ret == SYSERR) {
279  kprintf("\n[TFTP GET] Error on final ack\n");
280  return SYSERR;
281  }
282  return filesiz;
283  }
284 
285  /* Move to next block and continue */
286 
287  expected++;
288  }
289 }
syscall kprintf(char *fmt,...)
ポーリングI/Oを使用して、フォーマットされた文字列をコンソールに出力する。
Definition: kprintf.c:98
#define TFTP_MAXNAM
Definition: tftp.h:21
#define NULL
連結リスト用のNULLポインタ
Definition: kernel.h:68
char * strncpy(char *, const char *, int32)
文字列s1に文字列s2をN文字(Byte)分コピーする。
Definition: strncpy.c:15
#define SYSERR
処理が失敗した場合
Definition: kernel.h:79
status tftp_send1(int32 sock, uint32 remip, uint16 *remport, struct tftp_msg *msg, int32 mlen, struct tftp_msg *inmsg, uint16 expected)
Definition: tftp.c:16
#define TFTP_ACK
Definition: tftp.h:7
#define TIMEOUT
システムコールがタイムアウトした場合
Definition: kernel.h:83
Definition: tftp.h:33
int int32
符号あり32ビット整数(int)
Definition: kernel.h:11
#define TFTP_PORT
Definition: tftp.h:20
uid32 udp_register(uint32, uint16, uint16)
Definition: udp.c:85
unsigned short uint16
符号なし16ビット整数(unsigned short)
Definition: kernel.h:17
void * memset(void *, const int, int32)
指定のByteブロックに対して、同じ値をNバイト分書き込む。
Definition: memset.c:13
status udp_release(uid32)
Definition: udp.c:502
uint16 getport(void)
Definition: net.c:150
status udp_sendto(uid32, uint32, uint16, char *, int32)
Definition: udp.c:417
unsigned int uint32
符号なし32ビット整数(unsigned int)
Definition: kernel.h:15
#define htons(x)
Definition: prototypes.h:619
int32 strnlen(const char *, uint32)
NULL終端された文字列の長さを返す。NULL終端は長さに含まない。
Definition: strnlen.c:14
#define NULLCH
NULL文字(NULL終端)
Definition: kernel.h:70
#define TFTP_MAXRETRIES
Definition: tftp.h:23
#define TFTP_RRQ
Definition: tftp.h:4
#define TFTP_VERBOSE
Definition: tftp.h:28
Here is the call graph for this function:
Here is the caller graph for this function: