VirtualBox

source: vbox/trunk/src/VBox/NetworkServices/NAT/proxy_tftpd.c@ 50213

最後變更 在這個檔案從50213是 49016,由 vboxsync 提交於 11 年 前

Change vestigial names proxytest.* to proxy.*

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id Revision
檔案大小: 20.9 KB
 
1/* -*- indent-tabs-mode: nil; -*- */
2#define _USE_WINSTD_ERRNO
3/* XXX: replace POSIX file operations with IPRT, to avoid hacks with errno renamings */
4#include "winutils.h"
5
6#include "proxy.h"
7#include "tftp.h"
8
9#ifndef RT_OS_WINDOWS
10#include <sys/types.h>
11#include <sys/stat.h>
12#include <errno.h>
13#include <fcntl.h>
14#include <stdarg.h>
15#include <stdio.h>
16#include <string.h>
17#include <unistd.h>
18#else
19#include <sys/types.h>
20#include <sys/stat.h>
21#include <errno.h>
22#include <fcntl.h>
23#include <io.h>
24#include <stdarg.h>
25#include <stdio.h>
26#include <string.h>
27
28# define O_RDONLY _O_RDONLY
29# define S_ISREG(x) ((x) & _S_IFREG)
30#endif
31
32#include "lwip/timers.h"
33#include "lwip/udp.h"
34
35#include <iprt/string.h>
36
37struct xfer {
38 struct udp_pcb *pcb;
39 int fd;
40 unsigned int ack;
41 struct pbuf *pbuf;
42
43 struct pbuf *oack;
44
45 int rexmit;
46
47 ipX_addr_t peer_ip;
48 u16_t peer_port;
49
50 char *filename;
51 int octet;
52
53 /* options */
54 unsigned int blksize;
55 int blksize_from_opt;
56
57 unsigned int timeout;
58 int timeout_from_opt;
59
60 off_t tsize;
61 int tsize_from_opt;
62};
63
64struct tftpd {
65 struct udp_pcb *pcb;
66 char *root;
67
68#define TFTP_MAX_XFERS 3
69 struct xfer xfers[TFTP_MAX_XFERS];
70};
71
72struct tftp_option {
73 const char *name;
74 int (*getopt)(struct xfer *, const char *);
75 int (*ackopt)(struct xfer *, char **, size_t *);
76};
77
78
79static void tftpd_recv(void *, struct udp_pcb *, struct pbuf *, ip_addr_t *, u16_t);
80
81static void tftpd_rrq(struct pbuf *, ip_addr_t *, u16_t);
82
83static void tftp_xfer_recv(void *, struct udp_pcb *, struct pbuf *, ip_addr_t *, u16_t);
84
85static void tftp_recv_ack(struct xfer *, u16_t);
86static void tftp_fillbuf(struct xfer *);
87static void tftp_send(struct xfer *);
88static void tftp_timeout(void *);
89
90static struct xfer *tftp_xfer_alloc(ip_addr_t *, u16_t);
91static int tftp_xfer_create_pcb(struct xfer *);
92static void tftp_xfer_free(struct xfer *);
93
94static int tftp_parse_filename(struct xfer *, char **, size_t *);
95static int tftp_parse_mode(struct xfer *, char **, size_t *);
96static int tftp_parse_option(struct xfer *, char **, size_t *);
97
98static int tftp_opt_blksize(struct xfer *, const char *);
99static int tftp_opt_timeout(struct xfer *, const char *);
100static int tftp_opt_tsize(struct xfer *, const char *);
101
102static char *tftp_getstr(struct xfer *, const char *, char **, size_t *);
103
104static int tftp_ack_blksize(struct xfer *, char **, size_t *);
105static int tftp_ack_timeout(struct xfer *, char **, size_t *);
106static int tftp_ack_tsize(struct xfer *, char **, size_t *);
107
108static int tftp_add_oack(char **, size_t *, const char *, const char *, ...) __attribute__((format(printf, 4, 5)));
109
110static ssize_t tftp_strnlen(char *, size_t);
111
112static int tftp_internal_error(struct xfer *);
113static int tftp_error(struct xfer *, u16_t, const char *, ...) __attribute__((format(printf, 3, 4)));
114static void tftpd_error(ip_addr_t *, u16_t, u16_t, const char *, ...) __attribute__((format(printf, 4, 5)));
115static struct pbuf *tftp_verror(u16_t, const char *, va_list);
116
117
118/* const */ int report_transient_errors = 1;
119static struct tftpd tftpd;
120
121static struct tftp_option tftp_options[] = {
122 { "blksize", tftp_opt_blksize, tftp_ack_blksize }, /* RFC 2348 */
123 { "timeout", tftp_opt_timeout, tftp_ack_timeout }, /* RFC 2349 */
124 { "tsize", tftp_opt_tsize, tftp_ack_tsize }, /* RFC 2349 */
125 { NULL, NULL, NULL }
126};
127
128
129err_t
130tftpd_init(struct netif *proxy_netif, const char *tftproot)
131{
132 size_t len;
133 err_t error;
134
135 tftpd.root = strdup(tftproot);
136 if (tftpd.root == NULL) {
137 DPRINTF0(("%s: failed to allocate tftpd.root\n", __func__));
138 return ERR_MEM;
139 }
140
141 len = strlen(tftproot);
142 if (tftpd.root[len - 1] == '/') {
143 tftpd.root[len - 1] = '\0';
144 }
145
146 tftpd.pcb = udp_new();
147 if (tftpd.pcb == NULL) {
148 DPRINTF0(("%s: failed to allocate PCB\n", __func__));
149 return ERR_MEM;
150 }
151
152 udp_recv(tftpd.pcb, tftpd_recv, NULL);
153
154 error = udp_bind(tftpd.pcb, &proxy_netif->ip_addr, TFTP_SERVER_PORT);
155 if (error != ERR_OK) {
156 DPRINTF0(("%s: failed to bind PCB\n", __func__));
157 return error;
158 }
159
160 return ERR_OK;
161}
162
163
164static void
165tftpd_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p,
166 ip_addr_t *addr, u16_t port)
167{
168 u16_t op;
169
170 LWIP_ASSERT1(pcb == tftpd.pcb);
171
172 LWIP_UNUSED_ARG(pcb); /* only in assert */
173 LWIP_UNUSED_ARG(arg);
174
175 if (pbuf_clen(p) > 1) { /* this code assumes contiguous aligned payload */
176 pbuf_free(p);
177 return;
178 }
179
180 op = ntohs(*(u16_t *)p->payload);
181 switch (op) {
182 case TFTP_RRQ:
183 tftpd_rrq(p, addr, port);
184 break;
185
186 case TFTP_WRQ:
187 tftpd_error(addr, port, TFTP_EACCESS, "Permission denied");
188 break;
189
190 default:
191 tftpd_error(addr, port, TFTP_ENOSYS, "Bad opcode %d", op);
192 break;
193 }
194
195 pbuf_free(p);
196}
197
198
199/**
200 * Parse Read Request packet and start new transfer.
201 */
202static void
203tftpd_rrq(struct pbuf *p, ip_addr_t *addr, u16_t port)
204{
205 struct xfer *xfer;
206 char *s;
207 size_t len;
208 int has_options;
209 int status;
210
211 xfer = tftp_xfer_alloc(addr, port);
212 if (xfer == NULL) {
213 return;
214 }
215
216 /* skip opcode */
217 s = (char *)p->payload + sizeof(u16_t);
218 len = p->len - sizeof(u16_t);
219
220
221 /*
222 * Parse RRQ:
223 * filename, mode, [opt1, value1, [...] ]
224 */
225 status = tftp_parse_filename(xfer, &s, &len);
226 if (status < 0) {
227 goto terminate;
228 }
229
230 status = tftp_parse_mode(xfer, &s, &len);
231 if (status < 0) {
232 goto terminate;
233 }
234
235 has_options = 0;
236 while (len > 0) {
237 status = tftp_parse_option(xfer, &s, &len);
238 if (status < 0) {
239 goto terminate;
240 }
241 has_options += status;
242 }
243
244
245 /*
246 * Create OACK packet if necessary.
247 */
248 if (has_options) {
249 xfer->oack = pbuf_alloc(PBUF_RAW, 128, PBUF_RAM);
250 if (xfer->oack != NULL) {
251 struct tftp_option *o;
252
253 ((u16_t *)xfer->oack->payload)[0] = PP_HTONS(TFTP_OACK);
254
255 s = (char *)xfer->oack->payload + sizeof(u16_t);
256 len = xfer->oack->len - sizeof(u16_t);
257
258 for (o = &tftp_options[0]; o->name != NULL; ++o) {
259 status = (*o->ackopt)(xfer, &s, &len);
260 if (status < 0) {
261 pbuf_free(xfer->oack);
262 xfer->oack = NULL;
263 break;
264 }
265 }
266
267 if (xfer->oack != NULL) {
268 pbuf_realloc(xfer->oack, xfer->oack->len - len);
269 }
270 }
271 }
272
273
274 /*
275 * Create static pbuf that will be used for all data packets.
276 */
277 xfer->pbuf = pbuf_alloc(PBUF_RAW, xfer->blksize + 4, PBUF_RAM);
278 if (xfer->pbuf == NULL) {
279 tftp_internal_error(xfer);
280 goto terminate;
281 }
282 ((u16_t *)xfer->pbuf->payload)[0] = PP_HTONS(TFTP_DATA);
283
284
285 /*
286 * Finally, create PCB. Before this point any error was reported
287 * from the server port (see tftp_error() for the reason).
288 */
289 status = tftp_xfer_create_pcb(xfer);
290 if (status < 0) {
291 goto terminate;
292 }
293
294 if (xfer->oack) {
295 tftp_send(xfer);
296 }
297 else {
298 /* trigger send of the first data packet */
299 tftp_recv_ack(xfer, 0);
300 }
301
302 return;
303
304 terminate:
305 DPRINTF(("%s: terminated", __func__));
306 tftp_xfer_free(xfer);
307}
308
309
310static void
311tftp_xfer_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p,
312 ip_addr_t *addr, u16_t port)
313{
314 struct xfer *xfer = (struct xfer *)arg;
315 u16_t op;
316
317 LWIP_UNUSED_ARG(pcb); /* assert only */
318 LWIP_UNUSED_ARG(addr);
319 LWIP_UNUSED_ARG(port);
320
321 LWIP_ASSERT1(xfer->pcb == pcb);
322
323 if (p->len < 2) {
324 tftp_error(xfer, TFTP_ENOSYS, "Short packet");
325 tftp_xfer_free(xfer);
326 pbuf_free(p);
327 return;
328 }
329
330 op = ntohs(*(u16_t *)p->payload);
331 if (op == TFTP_ACK) {
332 u16_t ack;
333
334 if (p->len < 4) {
335 tftp_error(xfer, TFTP_ENOSYS, "Short packet");
336 tftp_xfer_free(xfer);
337 pbuf_free(p);
338 return;
339 }
340
341 ack = ntohs(((u16_t *)p->payload)[1]);
342 tftp_recv_ack(xfer, ack);
343 }
344 else if (op == TFTP_ERROR) {
345 tftp_xfer_free(xfer);
346 }
347 else {
348 tftp_error(xfer, TFTP_ENOSYS, "Unexpected opcode %d", op);
349 tftp_xfer_free(xfer);
350 }
351
352 pbuf_free(p);
353}
354
355
356static void
357tftp_recv_ack(struct xfer *xfer, u16_t ack)
358{
359 if (ack != (u16_t)xfer->ack) {
360 DPRINTF2(("%s: expect %u (%u), got %u\n",
361 __func__, (u16_t)xfer->ack, xfer->ack, ack));
362 return;
363 }
364
365 sys_untimeout(tftp_timeout, xfer);
366 xfer->rexmit = 0;
367
368 if (xfer->pbuf->len < xfer->blksize) {
369 DPRINTF(("%s: got final ack %u (%u)\n",
370 __func__, (u16_t)xfer->ack, xfer->ack));
371 tftp_xfer_free(xfer);
372 return;
373 }
374
375 if (xfer->oack != NULL) {
376 pbuf_free(xfer->oack);
377 xfer->oack = NULL;
378 }
379
380 ++xfer->ack;
381 tftp_fillbuf(xfer);
382 tftp_send(xfer);
383}
384
385
386static void
387tftp_send(struct xfer *xfer)
388{
389 struct pbuf *pbuf;
390
391 pbuf = xfer->oack ? xfer->oack : xfer->pbuf;
392 udp_send(xfer->pcb, pbuf);
393 sys_timeout(xfer->timeout * 1000, tftp_timeout, xfer);
394}
395
396
397static void
398tftp_timeout(void *arg)
399{
400 struct xfer *xfer = (struct xfer *)arg;
401 int maxrexmit;
402
403 maxrexmit = xfer->timeout < 60 ? 5 : 3;
404 if (++xfer->rexmit < maxrexmit) {
405 tftp_send(xfer);
406 }
407 else {
408 tftp_xfer_free(xfer);
409 }
410}
411
412
413static void
414tftp_fillbuf(struct xfer *xfer)
415{
416 ssize_t nread;
417
418 DPRINTF2(("%s: reading block %u\n", __func__, xfer->ack));
419
420 ((u16_t *)xfer->pbuf->payload)[1] = htons(xfer->ack);
421 nread = read(xfer->fd, (char *)xfer->pbuf->payload + 4, xfer->blksize);
422
423 if (nread < 0) {
424 tftp_error(xfer, TFTP_EUNDEF, "Read failed");
425 return;
426 }
427
428 pbuf_realloc(xfer->pbuf, nread + 4);
429}
430
431
432/**
433 * Find a free transfer slot (without a pcb). Record peer's IP
434 * address and port, but don't allocate a pcb yet.
435 *
436 * We delay creation of the pcb in response to the original request
437 * until the request is verified and accepted. This makes using
438 * tcpdump(8) easier, since tcpdump does not track TFTP transfers, so
439 * an error reply from a new pcb is not recognized as such and is not
440 * decoded as TFTP (see tftp_error()).
441 *
442 * If the request is rejected, the pcb remains NULL and the transfer
443 * slot remains unallocated. Since all TFTP processing happens on the
444 * lwIP thread, there's no concurrent processing, so we don't need to
445 * "lock" the transfer slot until the pcb is allocated.
446 */
447static struct xfer *
448tftp_xfer_alloc(ip_addr_t *addr, u16_t port)
449{
450 struct xfer *xfer;
451 int i;
452
453 /* Find free xfer slot */
454 xfer = NULL;
455 for (i = 0; i < TFTP_MAX_XFERS; ++i) {
456 if (tftpd.xfers[i].pcb == NULL) {
457 xfer = &tftpd.xfers[i];
458 break;
459 }
460 }
461
462 if (xfer == NULL) {
463 if (report_transient_errors) {
464 tftpd_error(addr, port, TFTP_EUNDEF,
465 "Maximum number of simultaneous connections exceeded");
466 }
467 return NULL;
468 }
469
470 ipX_addr_copy(0, xfer->peer_ip, *ip_2_ipX(addr));
471 xfer->peer_port = port;
472
473 xfer->ack = 0;
474
475 xfer->pbuf = NULL;
476 xfer->oack = NULL;
477 xfer->rexmit = 0;
478
479 xfer->blksize = 512;
480 xfer->blksize_from_opt = 0;
481
482 xfer->timeout = 1;
483 xfer->timeout_from_opt = 0;
484
485 xfer->tsize = -1;
486 xfer->tsize_from_opt = 0;
487
488 return xfer;
489}
490
491
492static int
493tftp_xfer_create_pcb(struct xfer *xfer)
494{
495 struct udp_pcb *pcb;
496 err_t error;
497
498 pcb = udp_new();
499
500 /* Bind */
501 if (pcb != NULL) {
502 error = udp_bind(pcb, ipX_2_ip(&tftpd.pcb->local_ip), 0);
503 if (error != ERR_OK) {
504 udp_remove(pcb);
505 pcb = NULL;
506 }
507 }
508
509 /* Connect */
510 if (pcb != NULL) {
511 error = udp_connect(pcb, ipX_2_ip(&xfer->peer_ip), xfer->peer_port);
512 if (error != ERR_OK) {
513 udp_remove(pcb);
514 pcb = NULL;
515 }
516 }
517
518 if (pcb == NULL) {
519 if (report_transient_errors) {
520 tftp_error(xfer, TFTP_EUNDEF, "Failed to create connection");
521 }
522 return -1;
523 }
524
525 xfer->pcb = pcb;
526 udp_recv(xfer->pcb, tftp_xfer_recv, xfer);
527
528 return 0;
529}
530
531
532static void
533tftp_xfer_free(struct xfer *xfer)
534{
535 sys_untimeout(tftp_timeout, xfer);
536
537 if (xfer->pcb != NULL) {
538 udp_remove(xfer->pcb);
539 xfer->pcb = NULL;
540 }
541
542 if (xfer->fd > 0) {
543 close(xfer->fd);
544 xfer->fd = -1;
545 }
546
547 if (xfer->oack != NULL) {
548 pbuf_free(xfer->oack);
549 xfer->oack = NULL;
550 }
551
552 if (xfer->pbuf != NULL) {
553 pbuf_free(xfer->pbuf);
554 xfer->pbuf = NULL;
555 }
556
557 if (xfer->filename != NULL) {
558 free(xfer->filename);
559 xfer->filename = NULL;
560 }
561}
562
563
564static int
565tftp_parse_filename(struct xfer *xfer, char **ps, size_t *plen)
566{
567 const char *filename;
568 struct stat st;
569 char *pathname;
570 char *s;
571 size_t len;
572 int status;
573
574 filename = tftp_getstr(xfer, "filename", ps, plen);
575 if (filename == NULL) {
576 return -1;
577 }
578
579 DPRINTF(("%s: requested file name: %s\n", __func__, filename));
580 xfer->filename = strdup(filename);
581 if (xfer->filename == NULL) {
582 return tftp_internal_error(xfer);
583 }
584
585 /* replace backslashes with forward slashes */
586 s = xfer->filename;
587 while ((s = strchr(s, '\\')) != NULL) {
588 *s++ = '/';
589 }
590
591 /* deny attempts to break out of tftp dir */
592 if (strncmp(xfer->filename, "../", 3) == 0
593 || strstr(xfer->filename, "/../") != NULL)
594 {
595 return tftp_error(xfer, TFTP_ENOENT, "Permission denied");
596 }
597
598 len = strlen(tftpd.root) + 1 /*slash*/ + strlen(xfer->filename) + 1 /*nul*/;
599 pathname = (char *)malloc(len);
600 if (pathname == NULL) {
601 return tftp_internal_error(xfer);
602 }
603
604 status = RTStrPrintf(pathname, len, "%s/%s", tftpd.root, xfer->filename);
605 if (status < 0) {
606 return tftp_internal_error(xfer);
607 }
608
609 DPRINTF(("%s: full pathname: %s\n", __func__, pathname));
610 xfer->fd = open(pathname, O_RDONLY);
611 if (xfer->fd < 0) {
612 if (errno == EPERM) {
613 return tftp_error(xfer, TFTP_ENOENT, "Permission denied");
614 }
615 else {
616 return tftp_error(xfer, TFTP_ENOENT, "File not found");
617 }
618 }
619
620 status = fstat(xfer->fd, &st);
621 if (status < 0) {
622 return tftp_internal_error(xfer);
623 }
624
625 if (!S_ISREG(st.st_mode)) {
626 return tftp_error(xfer, TFTP_ENOENT, "File not found");
627 }
628
629 xfer->tsize = st.st_size;
630 return 0;
631}
632
633
634static int
635tftp_parse_mode(struct xfer *xfer, char **ps, size_t *plen)
636{
637 const char *modename;
638
639 modename = tftp_getstr(xfer, "mode", ps, plen);
640 if (modename == NULL) {
641 return -1;
642 }
643
644 if (RTStrICmp(modename, "octet") == 0) {
645 xfer->octet = 1;
646 }
647 else if (RTStrICmp(modename, "netascii") == 0) {
648 xfer->octet = 0;
649 /* XXX: not (yet?) */
650 return tftp_error(xfer, TFTP_ENOSYS, "Mode \"netascii\" not supported");
651 }
652 else if (RTStrICmp(modename, "mail") == 0) {
653 return tftp_error(xfer, TFTP_ENOSYS, "Mode \"mail\" not supported");
654 }
655 else {
656 return tftp_error(xfer, TFTP_ENOSYS, "Unknown mode \"%s\"", modename);
657 }
658
659 return 0;
660}
661
662
663static int
664tftp_parse_option(struct xfer *xfer, char **ps, size_t *plen)
665{
666 const char *opt;
667 const char *val;
668 struct tftp_option *o;
669
670 opt = tftp_getstr(xfer, "option name", ps, plen);
671 if (opt == NULL) {
672 return -1;
673 }
674
675 if (*plen == 0) {
676 return tftp_error(xfer, TFTP_EUNDEF, "Missing option value");
677 }
678
679 val = tftp_getstr(xfer, "option value", ps, plen);
680 if (val == NULL) {
681 return -1;
682 }
683
684 /* handle option if known, ignore otherwise */
685 for (o = &tftp_options[0]; o->name != NULL; ++o) {
686 if (RTStrICmp(o->name, opt) == 0) {
687 return (*o->getopt)(xfer, val);
688 }
689 }
690
691 return 0; /* unknown option */
692}
693
694
695static int
696tftp_opt_blksize(struct xfer *xfer, const char *optval)
697{
698 char *end;
699 long blksize;
700
701 errno = 0;
702 blksize = strtol(optval, &end, 10);
703 if (errno != 0 || *end != '\0') {
704 return 0;
705 }
706
707 if (blksize < 8) {
708 return 0;
709 }
710
711 if (blksize > 1428) { /* exceeds ethernet mtu */
712 blksize = 1428;
713 }
714
715 xfer->blksize = blksize;
716 xfer->blksize_from_opt = 1;
717 return 1;
718}
719
720
721static int
722tftp_opt_timeout(struct xfer *xfer, const char *optval)
723{
724 LWIP_UNUSED_ARG(xfer);
725 LWIP_UNUSED_ARG(optval);
726 return 0;
727}
728
729
730static int
731tftp_opt_tsize(struct xfer *xfer, const char *optval)
732{
733 LWIP_UNUSED_ARG(optval); /* must be "0", but we don't check it */
734
735 if (xfer->tsize < 0) {
736 return 0;
737 }
738
739 xfer->tsize_from_opt = 1;
740 return 1;
741}
742
743
744static char *
745tftp_getstr(struct xfer *xfer, const char *msg, char **ps, size_t *plen)
746{
747 char *s;
748 ssize_t slen;
749
750 s = *ps;
751 slen = tftp_strnlen(s, *plen);
752 if (slen < 0) {
753 tftp_error(xfer, TFTP_EUNDEF, "Unterminated %s", msg);
754 return NULL;
755 }
756
757 *ps += slen + 1;
758 *plen -= slen + 1;
759
760 return s;
761}
762
763
764static int
765tftp_ack_blksize(struct xfer *xfer, char **ps, size_t *plen)
766{
767 if (!xfer->blksize_from_opt) {
768 return 0;
769 }
770
771 return tftp_add_oack(ps, plen, "blksize", "%u", xfer->blksize);
772}
773
774
775static int
776tftp_ack_timeout(struct xfer *xfer, char **ps, size_t *plen)
777{
778 if (!xfer->timeout_from_opt) {
779 return 0;
780 }
781
782 return tftp_add_oack(ps, plen, "timeout", "%u", xfer->timeout);
783}
784
785
786static int
787tftp_ack_tsize(struct xfer *xfer, char **ps, size_t *plen)
788{
789 if (!xfer->tsize_from_opt) {
790 return 0;
791 }
792
793 LWIP_ASSERT1(xfer->tsize >= 0);
794 return tftp_add_oack(ps, plen, "tsize",
795 /* XXX: FIXME: want 64 bit */
796 "%lu", (unsigned long)xfer->tsize);
797}
798
799
800static int
801tftp_add_oack(char **ps, size_t *plen,
802 const char *optname, const char *fmt, ...)
803{
804 va_list ap;
805 int sz;
806
807 sz = RTStrPrintf(*ps, *plen, "%s", optname);
808 if (sz < 0 || (size_t)sz >= *plen) {
809 return -1;
810 }
811
812 ++sz; /* for nul byte */
813 *ps += sz;
814 *plen -= sz;
815
816 va_start(ap, fmt);
817 sz = vsnprintf(*ps, *plen, fmt, ap);
818 va_end(ap);
819 if (sz < 0 || (size_t)sz >= *plen) {
820 return -1;
821 }
822
823 ++sz; /* for nul byte */
824 *ps += sz;
825 *plen -= sz;
826
827 return 0;
828}
829
830
831static ssize_t
832tftp_strnlen(char *buf, size_t bufsize)
833{
834 void *end;
835
836 end = memchr(buf, '\0', bufsize);
837 if (end == NULL) {
838 return -1;
839 }
840
841 return (char *)end - buf;
842}
843
844
845static int
846tftp_internal_error(struct xfer *xfer)
847{
848 if (report_transient_errors) {
849 tftp_error(xfer, TFTP_EUNDEF, "Internal error");
850 }
851 return -1;
852}
853
854
855/**
856 * Send an error packet to the peer.
857 *
858 * PCB may not be created yet in which case send the error packet from
859 * the TFTP server port (*).
860 *
861 * (*) We delay creation of the PCB in response to the original
862 * request until the request is verified and accepted. This makes
863 * using tcpdump(8) easier, since tcpdump does not track TFTP
864 * transfers, so an error reply from a new PCB is not recognized as
865 * such and is not decoded as TFTP.
866 *
867 * Always returns -1 for callers to reuse.
868 */
869static int
870tftp_error(struct xfer *xfer, u16_t error, const char *fmt, ...)
871{
872 va_list ap;
873 struct pbuf *q;
874
875 LWIP_ASSERT1(xfer != NULL);
876
877 va_start(ap, fmt);
878 q = tftp_verror(error, fmt, ap);
879 va_end(ap);
880
881 if (q == NULL) {
882 return -1;
883 }
884
885 if (xfer->pcb != NULL) {
886 udp_send(xfer->pcb, q);
887 }
888 else {
889 udp_sendto(tftpd.pcb, q, ipX_2_ip(&xfer->peer_ip), xfer->peer_port);
890 }
891
892 pbuf_free(q);
893 return -1;
894}
895
896
897/**
898 * Send an error packet from TFTP server port to the specified peer.
899 */
900static void
901tftpd_error(ip_addr_t *addr, u16_t port, u16_t error, const char *fmt, ...)
902{
903 va_list ap;
904 struct pbuf *q;
905
906 va_start(ap, fmt);
907 q = tftp_verror(error, fmt, ap);
908 va_end(ap);
909
910 if (q != NULL) {
911 udp_sendto(tftpd.pcb, q, addr, port);
912 pbuf_free(q);
913 }
914}
915
916
917/**
918 * Create ERROR pbuf with formatted error message.
919 */
920static struct pbuf *
921tftp_verror(u16_t error, const char *fmt, va_list ap)
922{
923 struct tftp_error {
924 u16_t opcode; /* TFTP_ERROR */
925 u16_t errcode;
926 char errmsg[512];
927 };
928
929 struct pbuf *p;
930 struct tftp_error *errpkt;
931 int msgsz;
932
933 p = pbuf_alloc(PBUF_TRANSPORT, sizeof(*errpkt), PBUF_RAM);
934 if (p == NULL) {
935 return NULL;
936 }
937
938 errpkt = (struct tftp_error *)p->payload;
939 errpkt->opcode = PP_HTONS(TFTP_ERROR);
940 errpkt->errcode = htons(error);
941
942 msgsz = vsnprintf(errpkt->errmsg, sizeof(errpkt->errmsg), fmt, ap);
943 if (msgsz < 0) {
944 errpkt->errmsg[0] = '\0';
945 msgsz = 1;
946 }
947 else if ((size_t)msgsz < sizeof(errpkt->errmsg)) {
948 ++msgsz; /* for nul byte */
949 }
950 else {
951 msgsz = sizeof(errpkt->errmsg); /* truncated, includes nul byte */
952 }
953
954 pbuf_realloc(p, sizeof(*errpkt) - sizeof(errpkt->errmsg) + msgsz);
955 return p;
956}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette