master
1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 *
9 * Trivial file transfer protocol server.
10 *
11 * This code includes many modifications by Jim Guyton <guyton@rand-unix>
12 *
13 * This source file was started based on netkit-tftpd 0.17
14 * Heavily modified for curl's test suite
15 */
17/*
18 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
19 * Copyright (c) 1983, Regents of the University of California.
20 * All rights reserved.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the above copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * This product includes software developed by the University of
33 * California, Berkeley and its contributors.
34 * 4. Neither the name of the University nor the names of its contributors
35 * may be used to endorse or promote products derived from this software
36 * without specific prior written permission.
37 *
38 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
39 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
42 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
43 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
44 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
46 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
47 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 *
50 * SPDX-License-Identifier: BSD-4-Clause-UC
51 */
52#include "first.h"
54#ifdef HAVE_SYS_IOCTL_H
55#include <sys/ioctl.h> /* for ioctl() */
56#endif
57#ifdef HAVE_FCNTL_H
58#include <fcntl.h>
59#endif
60#ifdef HAVE_SYS_FILIO_H
61#include <sys/filio.h> /* FIONREAD on Solaris 7 */
62#endif
64#include <setjmp.h> /* for sigjmp_buf, sigsetjmp() */
66#ifdef HAVE_PWD_H
67#include <pwd.h>
68#endif
70#include <ctype.h>
72/*****************************************************************************
73 * This is a rewrite/clone of the arpa/tftp.h file for systems without it. *
74 *****************************************************************************/
76#define SEGSIZE 512 /* data segment size */
77#define PKTSIZE (SEGSIZE + 4) /* SEGSIZE defined in arpa/tftp.h */
79#if defined(__GNUC__) && ((__GNUC__ >= 3) || \
80 ((__GNUC__ == 2) && defined(__GNUC_MINOR__) && (__GNUC_MINOR__ >= 7)))
81# define PACKED_STRUCT __attribute__((__packed__))
82#else
83# define PACKED_STRUCT /* NOTHING */
84#endif
86/* Using a packed struct as binary in a program is begging for problems, but
87 the tftpd server was written like this so we have this struct here to make
88 things build. */
90struct tftphdr {
91 unsigned short th_opcode; /* packet type */
92 unsigned short th_block; /* all sorts of things */
93 char th_data[1]; /* data or error string */
94} PACKED_STRUCT;
96#define th_stuff th_block
97#define th_code th_block
98#define th_msg th_data
100#define TFTP_EUNDEF 0
101#define TFTP_ENOTFOUND 1
102#define TFTP_EACCESS 2
103#define TFTP_ENOSPACE 3
104#define TFTP_EBADOP 4
105#define TFTP_EBADID 5
106#define TFTP_EEXISTS 6
107#define TFTP_ENOUSER 7
109/*****************************************************************************
110 * STRUCT DECLARATIONS AND DEFINES *
111 *****************************************************************************/
113struct testcase {
114 char *buffer; /* holds the file data to send to the client */
115 size_t bufsize; /* size of the data in buffer */
116 const char *rptr; /* read pointer into the buffer */
117 size_t rcount; /* amount of data left to read of the file */
118 long testno; /* test case number */
119 int ofile; /* file descriptor for output file when uploading to us */
121 int writedelay; /* number of seconds between each packet */
122};
124struct formats {
125 const char *f_mode;
126 int f_convert;
127};
129struct errmsg {
130 int e_code;
131 char e_msg[STRERROR_LEN];
132};
134typedef union {
135 struct tftphdr hdr;
136 char storage[PKTSIZE];
137} tftphdr_storage_t;
139/*
140 * bf.counter values in range [-1 .. SEGSIZE] represents size of data in the
141 * bf.buf buffer. Additionally it can also hold flags BF_ALLOC or BF_FREE.
142 */
144struct bf {
145 int counter; /* size of data in buffer, or flag */
146 tftphdr_storage_t buf; /* room for data packet */
147};
149#define BF_ALLOC (-3) /* allocated but not yet filled */
150#define BF_FREE (-2) /* free */
152#define opcode_RRQ 1
153#define opcode_WRQ 2
154#define opcode_DATA 3
155#define opcode_ACK 4
156#define opcode_ERROR 5
158#define TIMEOUT 5
160#define REQUEST_DUMP "server.input"
162/*****************************************************************************
163 * GLOBAL VARIABLES *
164 *****************************************************************************/
166static struct errmsg errmsgs[] = {
167 { TFTP_EUNDEF, "Undefined error code" },
168 { TFTP_ENOTFOUND, "File not found" },
169 { TFTP_EACCESS, "Access violation" },
170 { TFTP_ENOSPACE, "Disk full or allocation exceeded" },
171 { TFTP_EBADOP, "Illegal TFTP operation" },
172 { TFTP_EBADID, "Unknown transfer ID" },
173 { TFTP_EEXISTS, "File already exists" },
174 { TFTP_ENOUSER, "No such user" },
175 { -1, "" }
176};
178static const struct formats formata[] = {
179 { "netascii", 1 },
180 { "octet", 0 },
181 { NULL, 0 }
182};
184static struct bf bfs[2];
186static int nextone; /* index of next buffer to use */
187static int current; /* index of buffer in use */
189 /* control flags for CRLF conversions */
190static int newline = 0; /* fillbuf: in middle of newline expansion */
191static int prevchar = -1; /* putbuf: previous char (cr check) */
193static tftphdr_storage_t trsbuf;
194static tftphdr_storage_t ackbuf;
196static curl_socket_t peer = CURL_SOCKET_BAD;
198static unsigned int timeout;
199static unsigned int maxtimeout = TIMEOUT * 5;
201static int tftpd_wrotepidfile = 0;
202static int tftpd_wroteportfile = 0;
204#ifdef HAVE_SIGSETJMP
205static sigjmp_buf timeoutbuf;
206#endif
208/*****************************************************************************
209 * FUNCTION IMPLEMENTATIONS *
210 *****************************************************************************/
212#if defined(HAVE_ALARM) && defined(SIGALRM)
213static const unsigned int rexmtval = TIMEOUT;
215/*
216 * Like signal(), but with well-defined semantics.
217 */
218static void mysignal(int sig, void (*handler)(int))
219{
220 struct sigaction sa;
221 memset(&sa, 0, sizeof(sa));
222 sa.sa_handler = handler;
223 sigaction(sig, &sa, NULL);
224}
226#ifdef HAVE_SIGSETJMP
227CURL_NORETURN
228#endif
229static void timer(int signum)
230{
231 (void)signum;
233 logmsg("alarm!");
235 timeout += rexmtval;
236 if(timeout >= maxtimeout) {
237 if(tftpd_wrotepidfile) {
238 tftpd_wrotepidfile = 0;
239 unlink(pidname);
240 }
241 if(tftpd_wroteportfile) {
242 tftpd_wroteportfile = 0;
243 unlink(portname);
244 }
245 if(serverlogslocked) {
246 serverlogslocked = 0;
247 clear_advisor_read_lock(loglockfile);
248 }
249 exit(1);
250 }
251#ifdef HAVE_SIGSETJMP
252 siglongjmp(timeoutbuf, 1);
253#endif
254}
256static void justtimeout(int signum)
257{
258 (void)signum;
259}
261#endif /* HAVE_ALARM && SIGALRM */
263/*
264 * Send a nak packet (error message). Error code passed in is one of the
265 * standard TFTP codes, or a Unix errno offset by 100.
266 */
267static void nak(int error)
268{
269 struct tftphdr *tp;
270 int length;
271 struct errmsg *pe;
273 tp = &trsbuf.hdr;
274 tp->th_opcode = htons(opcode_ERROR);
275 tp->th_code = htons((unsigned short)error);
276 for(pe = errmsgs; pe->e_code >= 0; pe++)
277 if(pe->e_code == error)
278 break;
279 if(pe->e_code < 0) {
280 curlx_strerror(error - 100, pe->e_msg, sizeof(pe->e_msg));
281 tp->th_code = TFTP_EUNDEF; /* set 'undef' errorcode */
282 }
283 length = (int)strlen(pe->e_msg);
285 /* we use memcpy() instead of strcpy() in order to avoid buffer overflow
286 * report from glibc with FORTIFY_SOURCE */
287 memcpy(tp->th_msg, pe->e_msg, length + 1);
288 length += 5;
289 if(swrite(peer, &trsbuf.storage[0], length) != length)
290 logmsg("nak: fail");
291}
293/*
294 * init for either read-ahead or write-behind.
295 * zero for write-behind, one for read-head.
296 */
297static struct tftphdr *rw_init(int x)
298{
299 newline = 0; /* init CRLF flag */
300 prevchar = -1;
301 bfs[0].counter = BF_ALLOC; /* pass out the first buffer */
302 current = 0;
303 bfs[1].counter = BF_FREE;
304 nextone = x; /* ahead or behind? */
305 return &bfs[0].buf.hdr;
306}
308static struct tftphdr *w_init(void)
309{
310 return rw_init(0); /* write-behind */
311}
313static struct tftphdr *r_init(void)
314{
315 return rw_init(1); /* read-ahead */
316}
318/*
319 * fill the input buffer, doing ASCII conversions if requested
320 * conversions are lf -> cr, lf and cr -> cr, nul
321 */
322static void read_ahead(struct testcase *test,
323 int convert /* if true, convert to ASCII */)
324{
325 int i;
326 char *p;
327 int c;
328 struct bf *b;
329 struct tftphdr *dp;
331 b = &bfs[nextone]; /* look at "next" buffer */
332 if(b->counter != BF_FREE) /* nop if not free */
333 return;
334 nextone = !nextone; /* "incr" next buffer ptr */
336 dp = &b->buf.hdr;
338 if(convert == 0) {
339 /* The former file reading code did this:
340 b->counter = read(fileno(file), dp->th_data, SEGSIZE); */
341 size_t copy_n = CURLMIN(SEGSIZE, test->rcount);
342 memcpy(dp->th_data, test->rptr, copy_n);
344 /* decrease amount, advance pointer */
345 test->rcount -= copy_n;
346 test->rptr += copy_n;
347 b->counter = (int)copy_n;
348 return;
349 }
351 p = dp->th_data;
352 for(i = 0; i < SEGSIZE; i++) {
353 if(newline) {
354 if(prevchar == '\n')
355 c = '\n'; /* lf to cr,lf */
356 else
357 c = '\0'; /* cr to cr,nul */
358 newline = 0;
359 }
360 else {
361 if(test->rcount) {
362 c = (unsigned char)test->rptr[0];
363 test->rptr++;
364 test->rcount--;
365 }
366 else
367 break;
368 if(c == '\n' || c == '\r') {
369 prevchar = c;
370 c = '\r';
371 newline = 1;
372 }
373 }
374 *p++ = (char)c;
375 }
376 b->counter = (int)(p - dp->th_data);
377}
379/* Have emptied current buffer by sending to net and getting ack.
380 Free it and return next buffer filled with data.
381 */
382static int readit(struct testcase *test, struct tftphdr * volatile *dpp,
383 int convert /* if true, convert to ASCII */)
384{
385 struct bf *b;
387 bfs[current].counter = BF_FREE; /* free old one */
388 current = !current; /* "incr" current */
390 b = &bfs[current]; /* look at new buffer */
391 if(b->counter == BF_FREE) /* if empty */
392 read_ahead(test, convert); /* fill it */
394 *dpp = &b->buf.hdr; /* set caller's ptr */
395 return b->counter;
396}
398/*
399 * Output a buffer to a file, converting from netascii if requested.
400 * CR, NUL -> CR and CR, LF => LF.
401 * Note spec is undefined if we get CR as last byte of file or a
402 * CR followed by anything else. In this case we leave it alone.
403 */
404static ssize_t write_behind(struct testcase *test, int convert)
405{
406 char *writebuf;
407 int count;
408 int ct;
409 char *p;
410 int c; /* current character */
411 struct bf *b;
412 struct tftphdr *dp;
414 b = &bfs[nextone];
415 if(b->counter < -1) /* anything to flush? */
416 return 0; /* nop if nothing to do */
418 if(!test->ofile) {
419 char outfile[256];
420 snprintf(outfile, sizeof(outfile), "%s/upload.%ld", logdir, test->testno);
421 test->ofile = curlx_open(outfile, O_CREAT | O_RDWR | CURL_O_BINARY,
422#ifdef _WIN32
423 _S_IREAD | _S_IWRITE
424#else
425 S_IRUSR | S_IWUSR | S_IXUSR |
426 S_IRGRP | S_IWGRP | S_IXGRP |
427 S_IROTH | S_IWOTH | S_IXOTH
428#endif
429 );
430 if(test->ofile == -1) {
431 logmsg("Could not create and/or open file %s for upload!", outfile);
432 return -1; /* failure! */
433 }
434 }
435 else if(test->ofile <= 0) {
436 return -1; /* failure! */
437 }
439 count = b->counter; /* remember byte count */
440 b->counter = BF_FREE; /* reset flag */
441 dp = &b->buf.hdr;
442 nextone = !nextone; /* incr for next time */
443 writebuf = dp->th_data;
445 if(count <= 0)
446 return -1; /* nak logic? */
448 if(convert == 0)
449 return write(test->ofile, writebuf, count);
451 p = writebuf;
452 ct = count;
453 while(ct--) { /* loop over the buffer */
454 c = (unsigned char)*p++; /* pick up a character */
455 if(prevchar == '\r') { /* if prev char was cr */
456 if(c == '\n') /* if have cr,lf then */
457 curl_lseek(test->ofile, -1, SEEK_CUR); /* smash lf on top of the cr */
458 else if(c == '\0') /* if have cr,nul then */
459 goto skipit; /* skip over the putc */
460 /* else fall through and allow it */
461 }
462 /* formerly
463 putc(c, file); */
464 if(write(test->ofile, &c, 1) != 1)
465 break;
466skipit:
467 prevchar = c;
468 }
469 return count;
470}
472/* Update count associated with the buffer, get new buffer from the queue.
473 Calls write_behind only if next buffer not available.
474 */
475static int writeit(struct testcase *test, struct tftphdr * volatile *dpp,
476 int ct, int convert)
477{
478 bfs[current].counter = ct; /* set size of data to write */
479 current = !current; /* switch to other buffer */
480 if(bfs[current].counter != BF_FREE) /* if not free */
481 write_behind(test, convert); /* flush it */
482 bfs[current].counter = BF_ALLOC; /* mark as allocated */
483 *dpp = &bfs[current].buf.hdr;
484 return ct; /* this is a lie of course */
485}
487/* When an error has occurred, it is possible that the two sides are out of
488 * synch. Ie: that what I think is the other side's response to packet N is
489 * really their response to packet N-1.
490 *
491 * To try to prevent that, we flush all the input queued up for us on the
492 * network connection on our host.
493 *
494 * We return the number of packets we flushed (mostly for reporting when trace
495 * is active).
496 */
498static int synchnet(curl_socket_t f /* socket to flush */)
499{
500 int j = 0;
501 char rbuf[PKTSIZE];
502 srvr_sockaddr_union_t fromaddr;
503 curl_socklen_t fromaddrlen;
505 for(;;) {
506#ifdef HAVE_IOCTLSOCKET_CAMEL_FIONBIO
507 long i;
508 (void)IoctlSocket(f, FIONBIO, &i);
509#elif defined(HAVE_IOCTLSOCKET)
510 unsigned long i;
511 (void)ioctlsocket(f, FIONREAD, &i);
512#else
513 int i;
514 (void)ioctl(f, FIONREAD, &i);
515#endif
516 if(i) {
517 j++;
518#ifdef USE_IPV6
519 if(socket_domain == AF_INET6)
520 fromaddrlen = sizeof(fromaddr.sa6);
521 else
522#endif
523 fromaddrlen = sizeof(fromaddr.sa4);
524 (void)recvfrom(f, rbuf, sizeof(rbuf), 0, &fromaddr.sa, &fromaddrlen);
525 }
526 else
527 break;
528 }
529 return j;
530}
532/* Based on the testno, parse the correct server commands. */
533static int tftpd_parse_servercmd(struct testcase *req)
534{
535 FILE *stream;
536 int error;
538 stream = test2fopen(req->testno, logdir);
539 if(!stream) {
540 char errbuf[STRERROR_LEN];
541 error = errno;
542 logmsg("fopen() failed with error (%d) %s",
543 error, curlx_strerror(error, errbuf, sizeof(errbuf)));
544 logmsg(" Could not open test file %ld", req->testno);
545 return 1; /* done */
546 }
547 else {
548 char *orgcmd = NULL;
549 const char *cmd = NULL;
550 size_t cmdsize = 0;
551 int num = 0;
553 /* get the custom server control "commands" */
554 error = getpart(&orgcmd, &cmdsize, "reply", "servercmd", stream);
555 curlx_fclose(stream);
556 if(error) {
557 logmsg("getpart() failed with error (%d)", error);
558 return 1; /* done */
559 }
561 cmd = orgcmd;
562 while(cmd && cmdsize) {
563 const char *check;
564 if(sscanf(cmd, "writedelay: %d", &num) == 1) {
565 logmsg("instructed to delay %d secs between packets", num);
566 req->writedelay = num;
567 }
568 else {
569 logmsg("Unknown <servercmd> instruction found: %s", cmd);
570 }
571 /* try to deal with CRLF or LF */
572 check = strchr(cmd, '\r');
573 if(!check)
574 check = strchr(cmd, '\n');
576 if(check) {
577 /* get to the letter following the newline */
578 while((*check == '\r') || (*check == '\n'))
579 check++;
581 if(!*check)
582 /* if we reached a zero, get out */
583 break;
584 cmd = check;
585 }
586 else
587 break;
588 }
589 free(orgcmd);
590 }
592 return 0; /* OK! */
593}
595/*
596 * Validate file access.
597 */
598static int validate_access(struct testcase *test,
599 const char *filename, unsigned short mode)
600{
601 const char *ptr;
603 logmsg("trying to get file: %s mode %x", filename, mode);
605 if(!strncmp("verifiedserver", filename, 14)) {
606 char weare[128];
607 size_t count;
608 snprintf(weare, sizeof(weare), "WE ROOLZ: %ld\r\n", (long)our_getpid());
609 count = strlen(weare);
611 logmsg("Are-we-friendly question received");
612 test->buffer = curlx_strdup(weare);
613 test->rptr = test->buffer; /* set read pointer */
614 test->bufsize = count; /* set total count */
615 test->rcount = count; /* set data left to read */
616 return 0; /* fine */
617 }
619 /* find the last slash */
620 ptr = strrchr(filename, '/');
622 if(ptr) {
623 char partbuf[80] = "data";
624 long partno;
625 long testno;
626 const char *pval;
627 curl_off_t num;
628 FILE *stream;
630 ptr++; /* skip the slash */
632 /* skip all non-numericals following the slash */
633 while(*ptr && !ISDIGIT(*ptr))
634 ptr++;
636 /* get the number */
637 pval = ptr;
638 if(!curlx_str_number(&pval, &num, INT_MAX))
639 testno = (long)num;
640 else {
641 logmsg("tftpd: failed to read the test number from '%s'", filename);
642 return TFTP_EACCESS;
643 }
645 if(testno > 10000) {
646 partno = testno % 10000;
647 testno /= 10000;
648 }
649 else
650 partno = 0;
652 logmsg("requested test number %ld part %ld", testno, partno);
654 test->testno = testno;
656 (void)tftpd_parse_servercmd(test);
658 stream = test2fopen(testno, logdir);
660 if(partno)
661 snprintf(partbuf, sizeof(partbuf), "data%ld", partno);
663 if(!stream) {
664 char errbuf[STRERROR_LEN];
665 int error = errno;
666 logmsg("fopen() failed with error (%d) %s",
667 error, curlx_strerror(error, errbuf, sizeof(errbuf)));
668 logmsg("Could not open test file for test: %ld", testno);
669 return TFTP_EACCESS;
670 }
671 else {
672 size_t count;
673 int error = getpart(&test->buffer, &count, "reply", partbuf, stream);
674 curlx_fclose(stream);
675 if(error) {
676 logmsg("getpart() failed with error (%d)", error);
677 return TFTP_EACCESS;
678 }
679 if(test->buffer) {
680 test->rptr = test->buffer; /* set read pointer */
681 test->bufsize = count; /* set total count */
682 test->rcount = count; /* set data left to read */
683 }
684 else
685 return TFTP_EACCESS;
686 }
687 }
688 else {
689 logmsg("no slash found in path");
690 return TFTP_EACCESS; /* failure */
691 }
693 logmsg("file opened and all is good");
694 return 0;
695}
697/*
698 * Send the requested file.
699 */
700static void sendtftp(struct testcase *test, const struct formats *pf)
701{
702 int size;
703 ssize_t n;
704 /* These are volatile to live through a siglongjmp */
705 volatile unsigned short sendblock; /* block count */
706 struct tftphdr * volatile sdp = r_init(); /* data buffer */
707 struct tftphdr * const sap = &ackbuf.hdr; /* ack buffer */
709 sendblock = 1;
710#if defined(HAVE_ALARM) && defined(SIGALRM)
711 mysignal(SIGALRM, timer);
712#endif
713 do {
714 size = readit(test, (struct tftphdr * volatile *)&sdp, pf->f_convert);
715 if(size < 0) {
716 nak(errno + 100);
717 return;
718 }
719 sdp->th_opcode = htons(opcode_DATA);
720 sdp->th_block = htons(sendblock);
721 timeout = 0;
722#ifdef HAVE_SIGSETJMP
723 (void)sigsetjmp(timeoutbuf, 1);
724#endif
725 if(test->writedelay) {
726 logmsg("Pausing %d seconds before %d bytes", test->writedelay, size);
727 curlx_wait_ms(1000 * test->writedelay);
728 }
730send_data:
731 logmsg("write");
732 if(swrite(peer, sdp, size + 4) != size + 4) {
733 logmsg("write: fail");
734 return;
735 }
736 read_ahead(test, pf->f_convert);
737 for(;;) {
738#ifdef HAVE_ALARM
739 alarm(rexmtval); /* read the ack */
740#endif
741 logmsg("read");
742 n = sread(peer, &ackbuf.storage[0], sizeof(ackbuf.storage));
743 logmsg("read: %ld", (long)n);
744#ifdef HAVE_ALARM
745 alarm(0);
746#endif
747 if(got_exit_signal)
748 return;
749 if(n < 0) {
750 logmsg("read: fail");
751 return;
752 }
753 sap->th_opcode = ntohs(sap->th_opcode);
754 sap->th_block = ntohs(sap->th_block);
756 if(sap->th_opcode == opcode_ERROR) {
757 logmsg("got ERROR");
758 return;
759 }
761 if(sap->th_opcode == opcode_ACK) {
762 if(sap->th_block == sendblock) {
763 break;
764 }
765 /* Re-synchronize with the other side */
766 (void)synchnet(peer);
767 if(sap->th_block == (sendblock - 1)) {
768 goto send_data;
769 }
770 }
771 }
772 sendblock++;
773 } while(size == SEGSIZE);
774}
776/*
777 * Receive a file.
778 */
779static void recvtftp(struct testcase *test, const struct formats *pf)
780{
781 ssize_t n, size;
782 /* These are volatile to live through a siglongjmp */
783 volatile unsigned short recvblock; /* block count */
784 struct tftphdr * volatile rdp; /* data buffer */
785 struct tftphdr *rap; /* ack buffer */
787 recvblock = 0;
788 rdp = w_init();
789#if defined(HAVE_ALARM) && defined(SIGALRM)
790 mysignal(SIGALRM, timer);
791#endif
792 rap = &ackbuf.hdr;
793 do {
794 timeout = 0;
795 rap->th_opcode = htons(opcode_ACK);
796 rap->th_block = htons(recvblock);
797 recvblock++;
798#ifdef HAVE_SIGSETJMP
799 (void)sigsetjmp(timeoutbuf, 1);
800#endif
801send_ack:
802 logmsg("write");
803 if(swrite(peer, &ackbuf.storage[0], 4) != 4) {
804 logmsg("write: fail");
805 goto abort;
806 }
807 write_behind(test, pf->f_convert);
808 for(;;) {
809#ifdef HAVE_ALARM
810 alarm(rexmtval);
811#endif
812 logmsg("read");
813 n = sread(peer, rdp, PKTSIZE);
814 logmsg("read: %ld", (long)n);
815#ifdef HAVE_ALARM
816 alarm(0);
817#endif
818 if(got_exit_signal)
819 goto abort;
820 if(n < 0) { /* really? */
821 logmsg("read: fail");
822 goto abort;
823 }
824 rdp->th_opcode = ntohs(rdp->th_opcode);
825 rdp->th_block = ntohs(rdp->th_block);
826 if(rdp->th_opcode == opcode_ERROR)
827 goto abort;
828 if(rdp->th_opcode == opcode_DATA) {
829 if(rdp->th_block == recvblock) {
830 break; /* normal */
831 }
832 /* Re-synchronize with the other side */
833 (void)synchnet(peer);
834 if(rdp->th_block == (recvblock - 1))
835 goto send_ack; /* rexmit */
836 }
837 }
839 size = writeit(test, &rdp, (int)(n - 4), pf->f_convert);
840 if(size != (n - 4)) { /* ahem */
841 if(size < 0)
842 nak(errno + 100);
843 else
844 nak(TFTP_ENOSPACE);
845 goto abort;
846 }
847 } while(size == SEGSIZE);
848 write_behind(test, pf->f_convert);
849 /* close the output file as early as possible after upload completion */
850 if(test->ofile > 0) {
851 curlx_close(test->ofile);
852 test->ofile = 0;
853 }
855 rap->th_opcode = htons(opcode_ACK); /* send the "final" ack */
856 rap->th_block = htons(recvblock);
857 (void)swrite(peer, &ackbuf.storage[0], 4);
858#if defined(HAVE_ALARM) && defined(SIGALRM)
859 mysignal(SIGALRM, justtimeout); /* abort read on timeout */
860 alarm(rexmtval);
861#endif
862 /* normally times out and quits */
863 n = sread(peer, &trsbuf.storage[0], sizeof(trsbuf.storage));
864#ifdef HAVE_ALARM
865 alarm(0);
866#endif
867 if(got_exit_signal)
868 goto abort;
869 if(n >= 4 && /* if read some data */
870 rdp->th_opcode == opcode_DATA && /* and got a data block */
871 recvblock == rdp->th_block) { /* then my last ack was lost */
872 (void)swrite(peer, &ackbuf.storage[0], 4); /* resend final ack */
873 }
874abort:
875 /* make sure the output file is closed in case of abort */
876 if(test->ofile > 0) {
877 curlx_close(test->ofile);
878 test->ofile = 0;
879 }
880}
882/*
883 * Handle initial connection protocol.
884 */
885static int do_tftp(struct testcase *test, struct tftphdr *tp, ssize_t size)
886{
887 char *cp;
888 int first = 1, ecode;
889 const struct formats *pf;
890 const char *filename;
891 char *mode = NULL;
892#ifdef USE_WINSOCK
893 DWORD recvtimeout, recvtimeoutbak;
894#endif
895 const char *option = "mode"; /* mode is implicit */
896 int toggle = 1;
897 FILE *server;
898 char dumpfile[256];
900 snprintf(dumpfile, sizeof(dumpfile), "%s/%s", logdir, REQUEST_DUMP);
902 /* Open request dump file. */
903 server = curlx_fopen(dumpfile, "ab");
904 if(!server) {
905 char errbuf[STRERROR_LEN];
906 int error = errno;
907 logmsg("fopen() failed with error (%d) %s",
908 error, curlx_strerror(error, errbuf, sizeof(errbuf)));
909 logmsg("Error opening file '%s'", dumpfile);
910 return -1;
911 }
913 /* store input protocol */
914 fprintf(server, "opcode = %x\n", tp->th_opcode);
916 cp = (char *)&tp->th_stuff;
917 filename = cp;
918 do {
919 bool endofit = TRUE;
920 while(cp < &trsbuf.storage[size]) {
921 if(*cp == '\0') {
922 endofit = FALSE;
923 break;
924 }
925 cp++;
926 }
927 if(endofit)
928 /* no more options */
929 break;
931 /* before increasing pointer, make sure it is still within the legal
932 space */
933 if((cp + 1) < &trsbuf.storage[size]) {
934 ++cp;
935 if(first) {
936 /* store the mode since we need it later */
937 mode = cp;
938 first = 0;
939 }
940 if(toggle)
941 /* name/value pair: */
942 fprintf(server, "%s = %s\n", option, cp);
943 else {
944 /* store the name pointer */
945 option = cp;
946 }
947 toggle ^= 1;
948 }
949 else
950 /* No more options */
951 break;
952 } while(1);
954 if(*cp || !mode) {
955 nak(TFTP_EBADOP);
956 curlx_fclose(server);
957 return 3;
958 }
960 /* store input protocol */
961 fprintf(server, "filename = %s\n", filename);
963 for(cp = mode; cp && *cp; cp++)
964 if(ISUPPER(*cp))
965 *cp = (char)tolower((int)*cp);
967 /* store input protocol */
968 curlx_fclose(server);
970 for(pf = formata; pf->f_mode; pf++)
971 if(!strcmp(pf->f_mode, mode))
972 break;
973 if(!pf->f_mode) {
974 nak(TFTP_EBADOP);
975 return 2;
976 }
977 ecode = validate_access(test, filename, tp->th_opcode);
978 if(ecode) {
979 nak(ecode);
980 return 1;
981 }
983#ifdef USE_WINSOCK
984 recvtimeout = sizeof(recvtimeoutbak);
985 getsockopt(peer, SOL_SOCKET, SO_RCVTIMEO,
986 (char *)&recvtimeoutbak, (int *)&recvtimeout);
987 recvtimeout = TIMEOUT * 1000;
988 setsockopt(peer, SOL_SOCKET, SO_RCVTIMEO,
989 (const char *)&recvtimeout, sizeof(recvtimeout));
990#endif
992 if(tp->th_opcode == opcode_WRQ)
993 recvtftp(test, pf);
994 else
995 sendtftp(test, pf);
997#ifdef USE_WINSOCK
998 recvtimeout = recvtimeoutbak;
999 setsockopt(peer, SOL_SOCKET, SO_RCVTIMEO,
1000 (const char *)&recvtimeout, sizeof(recvtimeout));
1001#endif
1003 return 0;
1004}
1006static int test_tftpd(int argc, const char **argv)
1007{
1008 struct tftphdr *tp;
1009 ssize_t n = 0;
1010 int arg = 1;
1011 curl_socket_t sock = CURL_SOCKET_BAD;
1012 struct testcase test;
1013 int result = 0;
1014 srvr_sockaddr_union_t from;
1015 curl_socklen_t fromlen;
1017 memset(&test, 0, sizeof(test));
1019 pidname = ".tftpd.pid";
1020 serverlogfile = "log/tftpd.log";
1021 serverlogslocked = 0;
1022 server_port = 8999; /* UDP */
1024 while(argc > arg) {
1025 const char *opt;
1026 curl_off_t num;
1027 if(!strcmp("--version", argv[arg])) {
1028 printf("tftpd IPv4%s\n",
1029#ifdef USE_IPV6
1030 "/IPv6"
1031#else
1032 ""
1033#endif
1034 );
1035 return 0;
1036 }
1037 else if(!strcmp("--pidfile", argv[arg])) {
1038 arg++;
1039 if(argc > arg)
1040 pidname = argv[arg++];
1041 }
1042 else if(!strcmp("--portfile", argv[arg])) {
1043 arg++;
1044 if(argc > arg)
1045 portname = argv[arg++];
1046 }
1047 else if(!strcmp("--logfile", argv[arg])) {
1048 arg++;
1049 if(argc > arg)
1050 serverlogfile = argv[arg++];
1051 }
1052 else if(!strcmp("--logdir", argv[arg])) {
1053 arg++;
1054 if(argc > arg)
1055 logdir = argv[arg++];
1056 }
1057 else if(!strcmp("--ipv4", argv[arg])) {
1058 socket_type = "IPv4";
1059 socket_domain = AF_INET;
1060 arg++;
1061 }
1062 else if(!strcmp("--ipv6", argv[arg])) {
1063#ifdef USE_IPV6
1064 socket_type = "IPv6";
1065 socket_domain = AF_INET6;
1066#endif
1067 arg++;
1068 }
1069 else if(!strcmp("--port", argv[arg])) {
1070 arg++;
1071 if(argc > arg) {
1072 opt = argv[arg];
1073 if(!curlx_str_number(&opt, &num, 0xffff))
1074 server_port = (uint16_t)num;
1075 arg++;
1076 }
1077 }
1078 else if(!strcmp("--srcdir", argv[arg])) {
1079 arg++;
1080 if(argc > arg) {
1081 srcpath = argv[arg];
1082 arg++;
1083 }
1084 }
1085 else {
1086 puts("Usage: tftpd [option]\n"
1087 " --version\n"
1088 " --logfile [file]\n"
1089 " --logdir [directory]\n"
1090 " --pidfile [file]\n"
1091 " --portfile [file]\n"
1092 " --ipv4\n"
1093 " --ipv6\n"
1094 " --port [port]\n"
1095 " --srcdir [path]");
1096 return 0;
1097 }
1098 }
1100 snprintf(loglockfile, sizeof(loglockfile), "%s/%s/tftp-%s.lock",
1101 logdir, SERVERLOGS_LOCKDIR, socket_type);
1103 install_signal_handlers(TRUE);
1105 result = open_udp_sock(&sock, &server_port);
1106 if(result)
1107 goto tftpd_cleanup;
1109 tftpd_wrotepidfile = write_pidfile(pidname);
1110 if(!tftpd_wrotepidfile) {
1111 result = 1;
1112 goto tftpd_cleanup;
1113 }
1115 if(portname) {
1116 tftpd_wroteportfile = write_portfile(portname, server_port);
1117 if(!tftpd_wroteportfile) {
1118 result = 1;
1119 goto tftpd_cleanup;
1120 }
1121 }
1123 logmsg("Running %s version on port UDP/%d", socket_type, (int)server_port);
1125 for(;;) {
1126 fromlen = sizeof(from);
1127#ifdef USE_IPV6
1128 if(socket_domain == AF_INET6)
1129 fromlen = sizeof(from.sa6);
1130 else
1131#endif
1132 fromlen = sizeof(from.sa4);
1133 n = (ssize_t)recvfrom(sock, &trsbuf.storage[0], sizeof(trsbuf.storage), 0,
1134 &from.sa, &fromlen);
1135 if(got_exit_signal)
1136 break;
1137 if(n < 0) {
1138 logmsg("recvfrom");
1139 result = 3;
1140 break;
1141 }
1143 set_advisor_read_lock(loglockfile);
1144 serverlogslocked = 1;
1146#ifdef USE_IPV6
1147 if(socket_domain == AF_INET6) {
1148 from.sa6.sin6_family = AF_INET6;
1149 peer = socket(AF_INET6, SOCK_DGRAM, 0);
1150 if(peer == CURL_SOCKET_BAD) {
1151 logmsg("socket");
1152 result = 2;
1153 break;
1154 }
1155 if(connect(peer, &from.sa, sizeof(from.sa6)) < 0) {
1156 logmsg("connect: fail");
1157 result = 1;
1158 break;
1159 }
1160 }
1161 else
1162#endif
1163 {
1164 from.sa4.sin_family = AF_INET;
1165 peer = socket(AF_INET, SOCK_DGRAM, 0);
1166 if(peer == CURL_SOCKET_BAD) {
1167 logmsg("socket");
1168 result = 2;
1169 break;
1170 }
1171 if(connect(peer, &from.sa, sizeof(from.sa4)) < 0) {
1172 logmsg("connect: fail");
1173 result = 1;
1174 break;
1175 }
1176 }
1178 maxtimeout = TIMEOUT * 5;
1180 tp = &trsbuf.hdr;
1181 tp->th_opcode = ntohs(tp->th_opcode);
1182 if(tp->th_opcode == opcode_RRQ || tp->th_opcode == opcode_WRQ) {
1183 memset(&test, 0, sizeof(test));
1184 if(do_tftp(&test, tp, n) < 0)
1185 break;
1186 free(test.buffer);
1187 }
1188 sclose(peer);
1189 peer = CURL_SOCKET_BAD;
1191 if(got_exit_signal)
1192 break;
1194 if(serverlogslocked) {
1195 serverlogslocked = 0;
1196 clear_advisor_read_lock(loglockfile);
1197 }
1199 logmsg("end of one transfer");
1200 }
1202tftpd_cleanup:
1204 if(test.ofile > 0)
1205 curlx_close(test.ofile);
1207 if((peer != sock) && (peer != CURL_SOCKET_BAD))
1208 sclose(peer);
1210 if(sock != CURL_SOCKET_BAD)
1211 sclose(sock);
1213 if(got_exit_signal)
1214 logmsg("signalled to die");
1216 if(tftpd_wrotepidfile)
1217 unlink(pidname);
1218 if(tftpd_wroteportfile)
1219 unlink(portname);
1221 if(serverlogslocked) {
1222 serverlogslocked = 0;
1223 clear_advisor_read_lock(loglockfile);
1224 }
1226 restore_signal_handlers(TRUE);
1228 return result;
1229}
Diagnostics4 git calls · 47 ms
CallStatusSizeTime047 ms
discover200152 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200149 B · hit2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=149 git.response.cache=HIT git.response.duration_ms=2 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=149 git.pkt.payload.max=78 git.ls_refs.count=2 git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=42 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=10 git.pkt.data=8 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=293470 git.pkt.payload.max=65516 git.sideband.band1.bytes=293354 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=87 git.pack.bytes=293354 git.pack.ofs_delta=0 git.pack.ref_delta=1 git.pack.chain.max=1 git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0 git.pack.full=86Open pack map
fetch20011 KiB · hit42 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=11081 git.response.cache=HIT git.response.duration_ms=42 git.fetch.wants=0c56676e83e452bb395d04a9f48291a041b78dc5 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=11081 git.pkt.payload.max=11060 git.sideband.band1.bytes=11059 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
- Ref
- refs/heads/master
- Commit
- 31c911efe5652725e930f4d2e60c9060e5f6646d
- cf-ray
- a5e2e2952f794af4
- Request
- 9b80e1f6…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format