2005-09-15 16:32:59 -04:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Trivial file transfer protocol server.
|
|
|
|
*
|
|
|
|
* This code includes many modifications by Jim Guyton <guyton@rand-unix>
|
|
|
|
*
|
|
|
|
* This source file was started based on netkit-tftpd 0.17
|
|
|
|
* Heavily modified for curl's test suite
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2016-11-23 02:30:18 -05:00
|
|
|
* Copyright (c) 1983, 2016 Regents of the University of California.
|
2005-09-15 16:32:59 -04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2012-04-10 11:32:06 -04:00
|
|
|
#include "server_setup.h"
|
2008-02-27 19:55:06 -05:00
|
|
|
|
2005-09-15 17:50:50 -04:00
|
|
|
#ifdef HAVE_SYS_IOCTL_H
|
2005-09-15 16:32:59 -04:00
|
|
|
#include <sys/ioctl.h>
|
2005-09-15 17:50:50 -04:00
|
|
|
#endif
|
2008-02-27 19:55:06 -05:00
|
|
|
#ifdef HAVE_SIGNAL_H
|
2005-09-15 16:32:59 -04:00
|
|
|
#include <signal.h>
|
2008-02-27 19:55:06 -05:00
|
|
|
#endif
|
2005-09-15 17:50:50 -04:00
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETINET_IN_H
|
2005-09-15 16:32:59 -04:00
|
|
|
#include <netinet/in.h>
|
2005-09-15 17:50:50 -04:00
|
|
|
#endif
|
2009-04-27 19:59:41 -04:00
|
|
|
#ifdef HAVE_ARPA_INET_H
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
2005-09-15 17:50:50 -04:00
|
|
|
#ifdef HAVE_ARPA_TFTP_H
|
2005-09-15 16:32:59 -04:00
|
|
|
#include <arpa/tftp.h>
|
2006-01-03 17:19:15 -05:00
|
|
|
#else
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "tftp.h"
|
2005-09-15 17:50:50 -04:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETDB_H
|
2005-09-15 16:32:59 -04:00
|
|
|
#include <netdb.h>
|
2005-09-15 17:50:50 -04:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_FILIO_H
|
|
|
|
/* FIONREAD on Solaris 7 */
|
|
|
|
#include <sys/filio.h>
|
|
|
|
#endif
|
2005-09-15 16:32:59 -04:00
|
|
|
|
|
|
|
#include <setjmp.h>
|
2012-12-14 11:38:18 -05:00
|
|
|
|
2006-01-03 17:19:15 -05:00
|
|
|
#ifdef HAVE_PWD_H
|
2005-09-15 16:32:59 -04:00
|
|
|
#include <pwd.h>
|
2006-01-03 17:19:15 -05:00
|
|
|
#endif
|
2005-09-15 16:32:59 -04:00
|
|
|
|
2005-09-16 01:49:53 -04:00
|
|
|
#define ENABLE_CURLX_PRINTF
|
|
|
|
/* make the curlx header define all printf() functions to use the curlx_*
|
|
|
|
versions instead */
|
|
|
|
#include "curlx.h" /* from the private lib dir */
|
2005-09-15 16:32:59 -04:00
|
|
|
#include "getpart.h"
|
|
|
|
#include "util.h"
|
2010-11-19 13:20:38 -05:00
|
|
|
#include "server_sockaddr.h"
|
2005-09-15 16:32:59 -04:00
|
|
|
|
2013-01-03 20:50:28 -05:00
|
|
|
/* include memdebug.h last */
|
|
|
|
#include "memdebug.h"
|
2005-09-19 18:03:16 -04:00
|
|
|
|
2009-12-02 10:02:30 -05:00
|
|
|
/*****************************************************************************
|
|
|
|
* STRUCT DECLARATIONS AND DEFINES *
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2010-01-23 08:51:53 -05:00
|
|
|
#ifndef PKTSIZE
|
2009-12-02 10:02:30 -05:00
|
|
|
#define PKTSIZE (SEGSIZE + 4) /* SEGSIZE defined in arpa/tftp.h */
|
2010-01-23 08:51:53 -05:00
|
|
|
#endif
|
2008-09-26 07:21:22 -04:00
|
|
|
|
2005-09-15 16:32:59 -04:00
|
|
|
struct testcase {
|
|
|
|
char *buffer; /* holds the file data to send to the client */
|
|
|
|
size_t bufsize; /* size of the data in buffer */
|
|
|
|
char *rptr; /* read pointer into the buffer */
|
|
|
|
size_t rcount; /* amount of data left to read of the file */
|
2013-08-22 13:23:08 -04:00
|
|
|
long testno; /* test case number */
|
2005-09-15 16:32:59 -04:00
|
|
|
int ofile; /* file descriptor for output file when uploading to us */
|
2013-08-22 13:23:08 -04:00
|
|
|
|
|
|
|
int writedelay; /* number of seconds between each packet */
|
2005-09-15 16:32:59 -04:00
|
|
|
};
|
|
|
|
|
2009-12-02 10:02:30 -05:00
|
|
|
struct formats {
|
|
|
|
const char *f_mode;
|
|
|
|
int f_convert;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct errmsg {
|
|
|
|
int e_code;
|
|
|
|
const char *e_msg;
|
|
|
|
};
|
|
|
|
|
2010-11-19 13:20:38 -05:00
|
|
|
typedef union {
|
|
|
|
struct tftphdr hdr;
|
|
|
|
char storage[PKTSIZE];
|
|
|
|
} tftphdr_storage_t;
|
|
|
|
|
2009-12-02 10:02:30 -05:00
|
|
|
/*
|
|
|
|
* bf.counter values in range [-1 .. SEGSIZE] represents size of data in the
|
|
|
|
* bf.buf buffer. Additionally it can also hold flags BF_ALLOC or BF_FREE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct bf {
|
|
|
|
int counter; /* size of data in buffer, or flag */
|
2010-11-19 13:20:38 -05:00
|
|
|
tftphdr_storage_t buf; /* room for data packet */
|
2009-12-02 10:02:30 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
#define BF_ALLOC -3 /* alloc'd but not yet filled */
|
|
|
|
#define BF_FREE -2 /* free */
|
2005-09-15 16:32:59 -04:00
|
|
|
|
2008-10-23 10:34:08 -04:00
|
|
|
#define opcode_RRQ 1
|
|
|
|
#define opcode_WRQ 2
|
|
|
|
#define opcode_DATA 3
|
|
|
|
#define opcode_ACK 4
|
|
|
|
#define opcode_ERROR 5
|
2005-09-15 16:32:59 -04:00
|
|
|
|
2009-12-02 10:02:30 -05:00
|
|
|
#define TIMEOUT 5
|
2005-09-15 16:32:59 -04:00
|
|
|
|
2009-12-02 10:02:30 -05:00
|
|
|
#undef MIN
|
|
|
|
#define MIN(x,y) ((x)<(y)?(x):(y))
|
2005-09-15 16:32:59 -04:00
|
|
|
|
2009-12-02 10:02:30 -05:00
|
|
|
#ifndef DEFAULT_LOGFILE
|
|
|
|
#define DEFAULT_LOGFILE "log/tftpd.log"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define REQUEST_DUMP "log/server.input"
|
|
|
|
|
|
|
|
#define DEFAULT_PORT 8999 /* UDP */
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* GLOBAL VARIABLES *
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
static struct errmsg errmsgs[] = {
|
|
|
|
{ EUNDEF, "Undefined error code" },
|
|
|
|
{ ENOTFOUND, "File not found" },
|
|
|
|
{ EACCESS, "Access violation" },
|
|
|
|
{ ENOSPACE, "Disk full or allocation exceeded" },
|
|
|
|
{ EBADOP, "Illegal TFTP operation" },
|
|
|
|
{ EBADID, "Unknown transfer ID" },
|
|
|
|
{ EEXISTS, "File already exists" },
|
|
|
|
{ ENOUSER, "No such user" },
|
|
|
|
{ -1, 0 }
|
2008-09-20 08:33:02 -04:00
|
|
|
};
|
2009-12-02 10:02:30 -05:00
|
|
|
|
2008-09-20 08:33:02 -04:00
|
|
|
static struct formats formata[] = {
|
|
|
|
{ "netascii", 1 },
|
|
|
|
{ "octet", 0 },
|
|
|
|
{ NULL, 0 }
|
|
|
|
};
|
|
|
|
|
2009-12-02 10:02:30 -05:00
|
|
|
static struct bf bfs[2];
|
|
|
|
|
|
|
|
static int nextone; /* index of next buffer to use */
|
|
|
|
static int current; /* index of buffer in use */
|
2005-09-15 16:32:59 -04:00
|
|
|
|
2009-12-02 10:02:30 -05:00
|
|
|
/* control flags for crlf conversions */
|
|
|
|
static int newline = 0; /* fillbuf: in middle of newline expansion */
|
|
|
|
static int prevchar = -1; /* putbuf: previous char (cr check) */
|
2005-09-15 16:32:59 -04:00
|
|
|
|
2010-11-19 13:20:38 -05:00
|
|
|
static tftphdr_storage_t buf;
|
|
|
|
static tftphdr_storage_t ackbuf;
|
2009-12-02 10:02:30 -05:00
|
|
|
|
2010-11-19 13:20:38 -05:00
|
|
|
static srvr_sockaddr_union_t from;
|
2009-05-01 22:37:32 -04:00
|
|
|
static curl_socklen_t fromlen;
|
2005-09-15 16:32:59 -04:00
|
|
|
|
2009-12-02 10:02:30 -05:00
|
|
|
static curl_socket_t peer = CURL_SOCKET_BAD;
|
2005-09-15 16:32:59 -04:00
|
|
|
|
2017-05-08 14:09:32 -04:00
|
|
|
static unsigned int timeout;
|
|
|
|
static unsigned int maxtimeout = 5 * TIMEOUT;
|
2005-09-15 16:32:59 -04:00
|
|
|
|
2009-12-02 10:02:30 -05:00
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
static bool use_ipv6 = FALSE;
|
|
|
|
#endif
|
|
|
|
static const char *ipv_inuse = "IPv4";
|
|
|
|
|
|
|
|
const char *serverlogfile = DEFAULT_LOGFILE;
|
2017-05-04 14:19:10 -04:00
|
|
|
static const char *pidname = ".tftpd.pid";
|
2009-12-02 10:02:30 -05:00
|
|
|
static int serverlogslocked = 0;
|
|
|
|
static int wrotepidfile = 0;
|
|
|
|
|
|
|
|
#ifdef HAVE_SIGSETJMP
|
|
|
|
static sigjmp_buf timeoutbuf;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(HAVE_ALARM) && defined(SIGALRM)
|
2017-05-08 14:09:32 -04:00
|
|
|
static const unsigned int rexmtval = TIMEOUT;
|
2009-12-02 10:02:30 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* do-nothing macro replacement for systems which lack siginterrupt() */
|
|
|
|
|
|
|
|
#ifndef HAVE_SIGINTERRUPT
|
|
|
|
#define siginterrupt(x,y) do {} while(0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* vars used to keep around previous signal handlers */
|
|
|
|
|
|
|
|
typedef RETSIGTYPE (*SIGHANDLER_T)(int);
|
|
|
|
|
|
|
|
#ifdef SIGHUP
|
|
|
|
static SIGHANDLER_T old_sighup_handler = SIG_ERR;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef SIGPIPE
|
|
|
|
static SIGHANDLER_T old_sigpipe_handler = SIG_ERR;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef SIGINT
|
|
|
|
static SIGHANDLER_T old_sigint_handler = SIG_ERR;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef SIGTERM
|
|
|
|
static SIGHANDLER_T old_sigterm_handler = SIG_ERR;
|
|
|
|
#endif
|
|
|
|
|
2013-01-09 16:19:03 -05:00
|
|
|
#if defined(SIGBREAK) && defined(WIN32)
|
|
|
|
static SIGHANDLER_T old_sigbreak_handler = SIG_ERR;
|
|
|
|
#endif
|
|
|
|
|
2009-12-02 10:02:30 -05:00
|
|
|
/* var which if set indicates that the program should finish execution */
|
|
|
|
|
|
|
|
SIG_ATOMIC_T got_exit_signal = 0;
|
|
|
|
|
|
|
|
/* if next is set indicates the first signal handled in exit_signal_handler */
|
|
|
|
|
|
|
|
static volatile int exit_signal = 0;
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* FUNCTION PROTOTYPES *
|
|
|
|
*****************************************************************************/
|
2005-09-15 16:32:59 -04:00
|
|
|
|
|
|
|
static struct tftphdr *rw_init(int);
|
|
|
|
|
2009-12-02 10:02:30 -05:00
|
|
|
static struct tftphdr *w_init(void);
|
|
|
|
|
|
|
|
static struct tftphdr *r_init(void);
|
|
|
|
|
|
|
|
static void read_ahead(struct testcase *test, int convert);
|
|
|
|
|
|
|
|
static ssize_t write_behind(struct testcase *test, int convert);
|
|
|
|
|
|
|
|
static int synchnet(curl_socket_t);
|
|
|
|
|
|
|
|
static int do_tftp(struct testcase *test, struct tftphdr *tp, ssize_t size);
|
|
|
|
|
|
|
|
static int validate_access(struct testcase *test, const char *fname, int mode);
|
|
|
|
|
|
|
|
static void sendtftp(struct testcase *test, struct formats *pf);
|
|
|
|
|
|
|
|
static void recvtftp(struct testcase *test, struct formats *pf);
|
|
|
|
|
|
|
|
static void nak(int error);
|
|
|
|
|
|
|
|
#if defined(HAVE_ALARM) && defined(SIGALRM)
|
|
|
|
|
|
|
|
static void mysignal(int sig, void (*handler)(int));
|
|
|
|
|
|
|
|
static void timer(int signum);
|
|
|
|
|
|
|
|
static void justtimeout(int signum);
|
|
|
|
|
|
|
|
#endif /* HAVE_ALARM && SIGALRM */
|
|
|
|
|
|
|
|
static RETSIGTYPE exit_signal_handler(int signum);
|
|
|
|
|
|
|
|
static void install_signal_handlers(void);
|
|
|
|
|
|
|
|
static void restore_signal_handlers(void);
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* FUNCTION IMPLEMENTATIONS *
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#if defined(HAVE_ALARM) && defined(SIGALRM)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Like signal(), but with well-defined semantics.
|
|
|
|
*/
|
|
|
|
static void mysignal(int sig, void (*handler)(int))
|
|
|
|
{
|
|
|
|
struct sigaction sa;
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
sa.sa_handler = handler;
|
|
|
|
sigaction(sig, &sa, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void timer(int signum)
|
|
|
|
{
|
|
|
|
(void)signum;
|
|
|
|
|
|
|
|
logmsg("alarm!");
|
|
|
|
|
|
|
|
timeout += rexmtval;
|
|
|
|
if(timeout >= maxtimeout) {
|
|
|
|
if(wrotepidfile) {
|
|
|
|
wrotepidfile = 0;
|
|
|
|
unlink(pidname);
|
|
|
|
}
|
|
|
|
if(serverlogslocked) {
|
|
|
|
serverlogslocked = 0;
|
|
|
|
clear_advisor_read_lock(SERVERLOGS_LOCK);
|
|
|
|
}
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
#ifdef HAVE_SIGSETJMP
|
|
|
|
siglongjmp(timeoutbuf, 1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void justtimeout(int signum)
|
|
|
|
{
|
|
|
|
(void)signum;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAVE_ALARM && SIGALRM */
|
|
|
|
|
|
|
|
/* signal handler that will be triggered to indicate that the program
|
|
|
|
should finish its execution in a controlled manner as soon as possible.
|
|
|
|
The first time this is called it will set got_exit_signal to one and
|
|
|
|
store in exit_signal the signal that triggered its execution. */
|
|
|
|
|
|
|
|
static RETSIGTYPE exit_signal_handler(int signum)
|
|
|
|
{
|
2013-01-09 09:10:23 -05:00
|
|
|
int old_errno = errno;
|
2009-12-02 10:02:30 -05:00
|
|
|
if(got_exit_signal == 0) {
|
|
|
|
got_exit_signal = 1;
|
|
|
|
exit_signal = signum;
|
|
|
|
}
|
|
|
|
(void)signal(signum, exit_signal_handler);
|
2013-01-09 09:10:23 -05:00
|
|
|
errno = old_errno;
|
2009-12-02 10:02:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void install_signal_handlers(void)
|
|
|
|
{
|
|
|
|
#ifdef SIGHUP
|
|
|
|
/* ignore SIGHUP signal */
|
2016-12-13 19:29:44 -05:00
|
|
|
old_sighup_handler = signal(SIGHUP, SIG_IGN);
|
|
|
|
if(old_sighup_handler == SIG_ERR)
|
2013-01-09 09:10:23 -05:00
|
|
|
logmsg("cannot install SIGHUP handler: %s", strerror(errno));
|
2009-12-02 10:02:30 -05:00
|
|
|
#endif
|
|
|
|
#ifdef SIGPIPE
|
|
|
|
/* ignore SIGPIPE signal */
|
2016-12-13 19:29:44 -05:00
|
|
|
old_sigpipe_handler = signal(SIGPIPE, SIG_IGN);
|
|
|
|
if(old_sigpipe_handler == SIG_ERR)
|
2013-01-09 09:10:23 -05:00
|
|
|
logmsg("cannot install SIGPIPE handler: %s", strerror(errno));
|
2009-12-02 10:02:30 -05:00
|
|
|
#endif
|
|
|
|
#ifdef SIGINT
|
|
|
|
/* handle SIGINT signal with our exit_signal_handler */
|
2016-12-13 19:29:44 -05:00
|
|
|
old_sigint_handler = signal(SIGINT, exit_signal_handler);
|
|
|
|
if(old_sigint_handler == SIG_ERR)
|
2013-01-09 09:10:23 -05:00
|
|
|
logmsg("cannot install SIGINT handler: %s", strerror(errno));
|
2009-12-02 10:02:30 -05:00
|
|
|
else
|
|
|
|
siginterrupt(SIGINT, 1);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGTERM
|
|
|
|
/* handle SIGTERM signal with our exit_signal_handler */
|
2016-12-13 19:29:44 -05:00
|
|
|
old_sigterm_handler = signal(SIGTERM, exit_signal_handler);
|
|
|
|
if(old_sigterm_handler == SIG_ERR)
|
2013-01-09 09:10:23 -05:00
|
|
|
logmsg("cannot install SIGTERM handler: %s", strerror(errno));
|
2009-12-02 10:02:30 -05:00
|
|
|
else
|
|
|
|
siginterrupt(SIGTERM, 1);
|
|
|
|
#endif
|
2013-01-09 16:19:03 -05:00
|
|
|
#if defined(SIGBREAK) && defined(WIN32)
|
|
|
|
/* handle SIGBREAK signal with our exit_signal_handler */
|
2016-12-13 19:29:44 -05:00
|
|
|
old_sigbreak_handler = signal(SIGBREAK, exit_signal_handler);
|
|
|
|
if(old_sigbreak_handler == SIG_ERR)
|
2013-01-09 16:19:03 -05:00
|
|
|
logmsg("cannot install SIGBREAK handler: %s", strerror(errno));
|
|
|
|
else
|
|
|
|
siginterrupt(SIGBREAK, 1);
|
|
|
|
#endif
|
2009-12-02 10:02:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void restore_signal_handlers(void)
|
|
|
|
{
|
|
|
|
#ifdef SIGHUP
|
|
|
|
if(SIG_ERR != old_sighup_handler)
|
|
|
|
(void)signal(SIGHUP, old_sighup_handler);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGPIPE
|
|
|
|
if(SIG_ERR != old_sigpipe_handler)
|
|
|
|
(void)signal(SIGPIPE, old_sigpipe_handler);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGINT
|
|
|
|
if(SIG_ERR != old_sigint_handler)
|
|
|
|
(void)signal(SIGINT, old_sigint_handler);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGTERM
|
|
|
|
if(SIG_ERR != old_sigterm_handler)
|
|
|
|
(void)signal(SIGTERM, old_sigterm_handler);
|
|
|
|
#endif
|
2013-01-09 16:19:03 -05:00
|
|
|
#if defined(SIGBREAK) && defined(WIN32)
|
|
|
|
if(SIG_ERR != old_sigbreak_handler)
|
|
|
|
(void)signal(SIGBREAK, old_sigbreak_handler);
|
|
|
|
#endif
|
2009-12-02 10:02:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* init for either read-ahead or write-behind.
|
|
|
|
* zero for write-behind, one for read-head.
|
|
|
|
*/
|
|
|
|
static struct tftphdr *rw_init(int x)
|
|
|
|
{
|
|
|
|
newline = 0; /* init crlf flag */
|
2005-09-15 16:32:59 -04:00
|
|
|
prevchar = -1;
|
|
|
|
bfs[0].counter = BF_ALLOC; /* pass out the first buffer */
|
|
|
|
current = 0;
|
|
|
|
bfs[1].counter = BF_FREE;
|
|
|
|
nextone = x; /* ahead or behind? */
|
2010-11-19 13:20:38 -05:00
|
|
|
return &bfs[0].buf.hdr;
|
2005-09-15 16:32:59 -04:00
|
|
|
}
|
|
|
|
|
2009-12-02 10:02:30 -05:00
|
|
|
static struct tftphdr *w_init(void)
|
|
|
|
{
|
|
|
|
return rw_init(0); /* write-behind */
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct tftphdr *r_init(void)
|
|
|
|
{
|
|
|
|
return rw_init(1); /* read-ahead */
|
|
|
|
}
|
2005-09-15 16:32:59 -04:00
|
|
|
|
|
|
|
/* Have emptied current buffer by sending to net and getting ack.
|
|
|
|
Free it and return next buffer filled with data.
|
|
|
|
*/
|
|
|
|
static int readit(struct testcase *test, struct tftphdr **dpp,
|
2009-04-24 17:46:42 -04:00
|
|
|
int convert /* if true, convert to ascii */)
|
2005-09-15 16:32:59 -04:00
|
|
|
{
|
|
|
|
struct bf *b;
|
|
|
|
|
|
|
|
bfs[current].counter = BF_FREE; /* free old one */
|
|
|
|
current = !current; /* "incr" current */
|
|
|
|
|
|
|
|
b = &bfs[current]; /* look at new buffer */
|
2016-04-03 16:35:43 -04:00
|
|
|
if(b->counter == BF_FREE) /* if it's empty */
|
2009-04-24 17:46:42 -04:00
|
|
|
read_ahead(test, convert); /* fill it */
|
2005-09-15 16:32:59 -04:00
|
|
|
|
2010-11-19 13:20:38 -05:00
|
|
|
*dpp = &b->buf.hdr; /* set caller's ptr */
|
2005-09-15 16:32:59 -04:00
|
|
|
return b->counter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* fill the input buffer, doing ascii conversions if requested
|
2016-04-03 16:35:43 -04:00
|
|
|
* conversions are lf -> cr, lf and cr -> cr, nul
|
2005-09-15 16:32:59 -04:00
|
|
|
*/
|
|
|
|
static void read_ahead(struct testcase *test,
|
|
|
|
int convert /* if true, convert to ascii */)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char *p;
|
|
|
|
int c;
|
|
|
|
struct bf *b;
|
|
|
|
struct tftphdr *dp;
|
|
|
|
|
|
|
|
b = &bfs[nextone]; /* look at "next" buffer */
|
2016-04-03 16:35:43 -04:00
|
|
|
if(b->counter != BF_FREE) /* nop if not free */
|
2005-09-15 16:32:59 -04:00
|
|
|
return;
|
|
|
|
nextone = !nextone; /* "incr" next buffer ptr */
|
|
|
|
|
2010-11-19 13:20:38 -05:00
|
|
|
dp = &b->buf.hdr;
|
2005-09-15 16:32:59 -04:00
|
|
|
|
2016-04-03 16:35:43 -04:00
|
|
|
if(convert == 0) {
|
2005-09-15 16:32:59 -04:00
|
|
|
/* The former file reading code did this:
|
|
|
|
b->counter = read(fileno(file), dp->th_data, SEGSIZE); */
|
2005-12-05 09:10:48 -05:00
|
|
|
size_t copy_n = MIN(SEGSIZE, test->rcount);
|
2005-09-15 16:32:59 -04:00
|
|
|
memcpy(dp->th_data, test->rptr, copy_n);
|
|
|
|
|
|
|
|
/* decrease amount, advance pointer */
|
|
|
|
test->rcount -= copy_n;
|
|
|
|
test->rptr += copy_n;
|
2005-12-06 02:47:37 -05:00
|
|
|
b->counter = (int)copy_n;
|
2005-09-15 16:32:59 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = dp->th_data;
|
2016-04-03 16:35:43 -04:00
|
|
|
for(i = 0 ; i < SEGSIZE; i++) {
|
|
|
|
if(newline) {
|
|
|
|
if(prevchar == '\n')
|
2005-09-15 16:32:59 -04:00
|
|
|
c = '\n'; /* lf to cr,lf */
|
|
|
|
else
|
|
|
|
c = '\0'; /* cr to cr,nul */
|
|
|
|
newline = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(test->rcount) {
|
2017-09-09 17:09:06 -04:00
|
|
|
c = test->rptr[0];
|
2005-09-15 16:32:59 -04:00
|
|
|
test->rptr++;
|
|
|
|
test->rcount--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
2016-04-03 16:35:43 -04:00
|
|
|
if(c == '\n' || c == '\r') {
|
2005-09-15 16:32:59 -04:00
|
|
|
prevchar = c;
|
|
|
|
c = '\r';
|
|
|
|
newline = 1;
|
|
|
|
}
|
|
|
|
}
|
2007-02-02 12:16:06 -05:00
|
|
|
*p++ = (char)c;
|
2005-09-15 16:32:59 -04:00
|
|
|
}
|
|
|
|
b->counter = (int)(p - dp->th_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update count associated with the buffer, get new buffer from the queue.
|
|
|
|
Calls write_behind only if next buffer not available.
|
|
|
|
*/
|
2014-05-29 03:24:12 -04:00
|
|
|
static int writeit(struct testcase *test, struct tftphdr * volatile *dpp,
|
2005-09-15 16:32:59 -04:00
|
|
|
int ct, int convert)
|
|
|
|
{
|
|
|
|
bfs[current].counter = ct; /* set size of data to write */
|
|
|
|
current = !current; /* switch to other buffer */
|
2016-04-03 16:35:43 -04:00
|
|
|
if(bfs[current].counter != BF_FREE) /* if not free */
|
2005-09-15 16:32:59 -04:00
|
|
|
write_behind(test, convert); /* flush it */
|
|
|
|
bfs[current].counter = BF_ALLOC; /* mark as alloc'd */
|
2010-11-19 13:20:38 -05:00
|
|
|
*dpp = &bfs[current].buf.hdr;
|
2005-09-15 16:32:59 -04:00
|
|
|
return ct; /* this is a lie of course */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Output a buffer to a file, converting from netascii if requested.
|
2016-04-03 16:35:43 -04:00
|
|
|
* CR, NUL -> CR and CR, LF => LF.
|
2005-09-15 16:32:59 -04:00
|
|
|
* Note spec is undefined if we get CR as last byte of file or a
|
|
|
|
* CR followed by anything else. In this case we leave it alone.
|
|
|
|
*/
|
2005-12-08 11:43:32 -05:00
|
|
|
static ssize_t write_behind(struct testcase *test, int convert)
|
2005-09-15 16:32:59 -04:00
|
|
|
{
|
2007-09-26 21:45:22 -04:00
|
|
|
char *writebuf;
|
2005-09-15 16:32:59 -04:00
|
|
|
int count;
|
|
|
|
int ct;
|
|
|
|
char *p;
|
|
|
|
int c; /* current character */
|
|
|
|
struct bf *b;
|
|
|
|
struct tftphdr *dp;
|
|
|
|
|
|
|
|
b = &bfs[nextone];
|
2016-04-03 16:35:43 -04:00
|
|
|
if(b->counter < -1) /* anything to flush? */
|
2005-09-15 16:32:59 -04:00
|
|
|
return 0; /* just nop if nothing to do */
|
|
|
|
|
|
|
|
if(!test->ofile) {
|
|
|
|
char outfile[256];
|
2013-08-22 13:23:08 -04:00
|
|
|
snprintf(outfile, sizeof(outfile), "log/upload.%ld", test->testno);
|
2015-12-23 07:04:00 -05:00
|
|
|
#ifdef WIN32
|
2017-09-09 17:09:06 -04:00
|
|
|
test->ofile = open(outfile, O_CREAT|O_RDWR|O_BINARY, 0777);
|
2015-12-23 07:04:00 -05:00
|
|
|
#else
|
2017-09-09 17:09:06 -04:00
|
|
|
test->ofile = open(outfile, O_CREAT|O_RDWR, 0777);
|
2015-12-23 07:04:00 -05:00
|
|
|
#endif
|
2005-09-15 16:32:59 -04:00
|
|
|
if(test->ofile == -1) {
|
|
|
|
logmsg("Couldn't create and/or open file %s for upload!", outfile);
|
|
|
|
return -1; /* failure! */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
count = b->counter; /* remember byte count */
|
|
|
|
b->counter = BF_FREE; /* reset flag */
|
2010-11-19 13:20:38 -05:00
|
|
|
dp = &b->buf.hdr;
|
2005-09-15 16:32:59 -04:00
|
|
|
nextone = !nextone; /* incr for next time */
|
2007-09-26 21:45:22 -04:00
|
|
|
writebuf = dp->th_data;
|
2005-09-15 16:32:59 -04:00
|
|
|
|
2016-04-03 16:35:43 -04:00
|
|
|
if(count <= 0)
|
2005-09-15 16:32:59 -04:00
|
|
|
return -1; /* nak logic? */
|
|
|
|
|
2016-04-03 16:35:43 -04:00
|
|
|
if(convert == 0)
|
2007-09-26 21:45:22 -04:00
|
|
|
return write(test->ofile, writebuf, count);
|
2005-09-15 16:32:59 -04:00
|
|
|
|
2007-09-26 21:45:22 -04:00
|
|
|
p = writebuf;
|
2005-09-15 16:32:59 -04:00
|
|
|
ct = count;
|
2016-04-03 16:35:43 -04:00
|
|
|
while(ct--) { /* loop over the buffer */
|
2005-09-15 16:32:59 -04:00
|
|
|
c = *p++; /* pick up a character */
|
2016-04-03 16:35:43 -04:00
|
|
|
if(prevchar == '\r') { /* if prev char was cr */
|
|
|
|
if(c == '\n') /* if have cr,lf then just */
|
2005-09-15 16:32:59 -04:00
|
|
|
lseek(test->ofile, -1, SEEK_CUR); /* smash lf on top of the cr */
|
|
|
|
else
|
2016-04-03 16:35:43 -04:00
|
|
|
if(c == '\0') /* if have cr,nul then */
|
2005-09-15 16:32:59 -04:00
|
|
|
goto skipit; /* just skip over the putc */
|
|
|
|
/* else just fall through and allow it */
|
|
|
|
}
|
|
|
|
/* formerly
|
|
|
|
putc(c, file); */
|
2011-06-11 17:01:09 -04:00
|
|
|
if(1 != write(test->ofile, &c, 1))
|
|
|
|
break;
|
2005-09-15 16:32:59 -04:00
|
|
|
skipit:
|
|
|
|
prevchar = c;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* When an error has occurred, it is possible that the two sides are out of
|
|
|
|
* synch. Ie: that what I think is the other side's response to packet N is
|
|
|
|
* really their response to packet N-1.
|
|
|
|
*
|
|
|
|
* So, to try to prevent that, we flush all the input queued up for us on the
|
|
|
|
* network connection on our host.
|
|
|
|
*
|
|
|
|
* We return the number of packets we flushed (mostly for reporting when trace
|
|
|
|
* is active).
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int synchnet(curl_socket_t f /* socket to flush */)
|
|
|
|
{
|
2006-01-03 17:19:15 -05:00
|
|
|
|
|
|
|
#if defined(HAVE_IOCTLSOCKET)
|
|
|
|
unsigned long i;
|
|
|
|
#else
|
|
|
|
int i;
|
|
|
|
#endif
|
|
|
|
int j = 0;
|
2005-09-15 16:32:59 -04:00
|
|
|
char rbuf[PKTSIZE];
|
2010-11-19 13:20:38 -05:00
|
|
|
srvr_sockaddr_union_t fromaddr;
|
2009-05-01 22:37:32 -04:00
|
|
|
curl_socklen_t fromaddrlen;
|
2005-09-15 16:32:59 -04:00
|
|
|
|
2016-04-03 16:35:43 -04:00
|
|
|
for(;;) {
|
2006-01-03 17:19:15 -05:00
|
|
|
#if defined(HAVE_IOCTLSOCKET)
|
|
|
|
(void) ioctlsocket(f, FIONREAD, &i);
|
2006-01-03 17:44:58 -05:00
|
|
|
#else
|
2005-09-15 16:32:59 -04:00
|
|
|
(void) ioctl(f, FIONREAD, &i);
|
2006-01-03 17:19:15 -05:00
|
|
|
#endif
|
2016-04-03 16:35:43 -04:00
|
|
|
if(i) {
|
2005-09-15 16:32:59 -04:00
|
|
|
j++;
|
2010-11-19 13:20:38 -05:00
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
if(!use_ipv6)
|
|
|
|
#endif
|
|
|
|
fromaddrlen = sizeof(fromaddr.sa4);
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
else
|
|
|
|
fromaddrlen = sizeof(fromaddr.sa6);
|
|
|
|
#endif
|
|
|
|
(void) recvfrom(f, rbuf, sizeof(rbuf), 0,
|
|
|
|
&fromaddr.sa, &fromaddrlen);
|
2005-09-15 16:32:59 -04:00
|
|
|
}
|
|
|
|
else
|
2005-12-19 14:47:14 -05:00
|
|
|
break;
|
2005-09-15 16:32:59 -04:00
|
|
|
}
|
2005-12-19 14:47:14 -05:00
|
|
|
return j;
|
2005-09-15 16:32:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2010-11-19 13:20:38 -05:00
|
|
|
srvr_sockaddr_union_t me;
|
2005-09-15 16:32:59 -04:00
|
|
|
struct tftphdr *tp;
|
2008-07-20 23:06:07 -04:00
|
|
|
ssize_t n = 0;
|
2005-09-15 16:32:59 -04:00
|
|
|
int arg = 1;
|
|
|
|
unsigned short port = DEFAULT_PORT;
|
2009-11-23 11:11:50 -05:00
|
|
|
curl_socket_t sock = CURL_SOCKET_BAD;
|
2005-09-15 16:32:59 -04:00
|
|
|
int flag;
|
|
|
|
int rc;
|
2009-11-23 11:11:50 -05:00
|
|
|
int error;
|
2009-12-02 10:02:30 -05:00
|
|
|
long pid;
|
2005-09-15 16:32:59 -04:00
|
|
|
struct testcase test;
|
2008-04-23 19:55:34 -04:00
|
|
|
int result = 0;
|
2005-09-15 16:32:59 -04:00
|
|
|
|
2009-12-03 15:37:49 -05:00
|
|
|
memset(&test, 0, sizeof(test));
|
|
|
|
|
2005-09-15 16:32:59 -04:00
|
|
|
while(argc>arg) {
|
|
|
|
if(!strcmp("--version", argv[arg])) {
|
|
|
|
printf("tftpd IPv4%s\n",
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
"/IPv6"
|
|
|
|
#else
|
|
|
|
""
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if(!strcmp("--pidfile", argv[arg])) {
|
|
|
|
arg++;
|
|
|
|
if(argc>arg)
|
|
|
|
pidname = argv[arg++];
|
|
|
|
}
|
2010-01-07 20:48:54 -05:00
|
|
|
else if(!strcmp("--logfile", argv[arg])) {
|
|
|
|
arg++;
|
|
|
|
if(argc>arg)
|
|
|
|
serverlogfile = argv[arg++];
|
|
|
|
}
|
|
|
|
else if(!strcmp("--ipv4", argv[arg])) {
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
ipv_inuse = "IPv4";
|
|
|
|
use_ipv6 = FALSE;
|
|
|
|
#endif
|
|
|
|
arg++;
|
|
|
|
}
|
2005-09-15 16:32:59 -04:00
|
|
|
else if(!strcmp("--ipv6", argv[arg])) {
|
|
|
|
#ifdef ENABLE_IPV6
|
2008-09-26 07:21:22 -04:00
|
|
|
ipv_inuse = "IPv6";
|
|
|
|
use_ipv6 = TRUE;
|
2005-09-15 16:32:59 -04:00
|
|
|
#endif
|
|
|
|
arg++;
|
|
|
|
}
|
2010-01-07 20:48:54 -05:00
|
|
|
else if(!strcmp("--port", argv[arg])) {
|
|
|
|
arg++;
|
|
|
|
if(argc>arg) {
|
2010-02-04 12:17:19 -05:00
|
|
|
char *endptr;
|
2010-02-18 07:31:24 -05:00
|
|
|
unsigned long ulnum = strtoul(argv[arg], &endptr, 10);
|
2010-02-04 12:17:19 -05:00
|
|
|
if((endptr != argv[arg] + strlen(argv[arg])) ||
|
2010-02-18 07:31:24 -05:00
|
|
|
(ulnum < 1025UL) || (ulnum > 65535UL)) {
|
2010-02-04 12:17:19 -05:00
|
|
|
fprintf(stderr, "tftpd: invalid --port argument (%s)\n",
|
|
|
|
argv[arg]);
|
|
|
|
return 0;
|
|
|
|
}
|
2010-02-22 13:56:29 -05:00
|
|
|
port = curlx_ultous(ulnum);
|
2010-01-07 20:48:54 -05:00
|
|
|
arg++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(!strcmp("--srcdir", argv[arg])) {
|
|
|
|
arg++;
|
|
|
|
if(argc>arg) {
|
|
|
|
path = argv[arg];
|
|
|
|
arg++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
puts("Usage: tftpd [option]\n"
|
|
|
|
" --version\n"
|
|
|
|
" --logfile [file]\n"
|
|
|
|
" --pidfile [file]\n"
|
|
|
|
" --ipv4\n"
|
|
|
|
" --ipv6\n"
|
|
|
|
" --port [port]\n"
|
2010-01-08 07:01:35 -05:00
|
|
|
" --srcdir [path]");
|
2010-01-07 20:48:54 -05:00
|
|
|
return 0;
|
2005-09-15 16:32:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-11 12:01:16 -04:00
|
|
|
#ifdef WIN32
|
2005-09-15 16:32:59 -04:00
|
|
|
win32_init();
|
|
|
|
atexit(win32_cleanup);
|
|
|
|
#endif
|
|
|
|
|
2009-12-02 10:02:30 -05:00
|
|
|
install_signal_handlers();
|
|
|
|
|
|
|
|
pid = (long)getpid();
|
|
|
|
|
2005-09-15 16:32:59 -04:00
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
if(!use_ipv6)
|
|
|
|
#endif
|
|
|
|
sock = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
else
|
|
|
|
sock = socket(AF_INET6, SOCK_DGRAM, 0);
|
|
|
|
#endif
|
|
|
|
|
2009-06-11 13:46:33 -04:00
|
|
|
if(CURL_SOCKET_BAD == sock) {
|
2009-11-23 11:11:50 -05:00
|
|
|
error = SOCKERRNO;
|
|
|
|
logmsg("Error creating socket: (%d) %s",
|
|
|
|
error, strerror(error));
|
2009-12-02 10:02:30 -05:00
|
|
|
result = 1;
|
|
|
|
goto tftpd_cleanup;
|
2005-09-15 16:32:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
flag = 1;
|
2016-04-03 16:35:43 -04:00
|
|
|
if(0 != setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
|
2009-11-23 11:11:50 -05:00
|
|
|
(void *)&flag, sizeof(flag))) {
|
|
|
|
error = SOCKERRNO;
|
|
|
|
logmsg("setsockopt(SO_REUSEADDR) failed with error: (%d) %s",
|
|
|
|
error, strerror(error));
|
2009-12-02 10:02:30 -05:00
|
|
|
result = 1;
|
|
|
|
goto tftpd_cleanup;
|
2005-09-15 16:32:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
if(!use_ipv6) {
|
|
|
|
#endif
|
2010-11-19 13:20:38 -05:00
|
|
|
memset(&me.sa4, 0, sizeof(me.sa4));
|
|
|
|
me.sa4.sin_family = AF_INET;
|
|
|
|
me.sa4.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
me.sa4.sin_port = htons(port);
|
|
|
|
rc = bind(sock, &me.sa, sizeof(me.sa4));
|
2005-09-15 16:32:59 -04:00
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
}
|
|
|
|
else {
|
2010-11-19 13:20:38 -05:00
|
|
|
memset(&me.sa6, 0, sizeof(me.sa6));
|
|
|
|
me.sa6.sin6_family = AF_INET6;
|
|
|
|
me.sa6.sin6_addr = in6addr_any;
|
|
|
|
me.sa6.sin6_port = htons(port);
|
|
|
|
rc = bind(sock, &me.sa, sizeof(me.sa6));
|
2005-09-15 16:32:59 -04:00
|
|
|
}
|
|
|
|
#endif /* ENABLE_IPV6 */
|
2009-11-23 11:11:50 -05:00
|
|
|
if(0 != rc) {
|
|
|
|
error = SOCKERRNO;
|
|
|
|
logmsg("Error binding socket on port %hu: (%d) %s",
|
|
|
|
port, error, strerror(error));
|
2009-12-02 10:02:30 -05:00
|
|
|
result = 1;
|
|
|
|
goto tftpd_cleanup;
|
2005-09-15 16:32:59 -04:00
|
|
|
}
|
|
|
|
|
2009-12-02 10:02:30 -05:00
|
|
|
wrotepidfile = write_pidfile(pidname);
|
|
|
|
if(!wrotepidfile) {
|
|
|
|
result = 1;
|
|
|
|
goto tftpd_cleanup;
|
2007-02-18 21:03:58 -05:00
|
|
|
}
|
2005-09-15 16:32:59 -04:00
|
|
|
|
2008-09-26 07:21:22 -04:00
|
|
|
logmsg("Running %s version on port UDP/%d", ipv_inuse, (int)port);
|
2005-09-15 16:32:59 -04:00
|
|
|
|
2016-04-03 16:35:43 -04:00
|
|
|
for(;;) {
|
2008-07-30 01:10:42 -04:00
|
|
|
fromlen = sizeof(from);
|
2010-11-19 13:20:38 -05:00
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
if(!use_ipv6)
|
|
|
|
#endif
|
|
|
|
fromlen = sizeof(from.sa4);
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
else
|
|
|
|
fromlen = sizeof(from.sa6);
|
|
|
|
#endif
|
|
|
|
n = (ssize_t)recvfrom(sock, &buf.storage[0], sizeof(buf.storage), 0,
|
|
|
|
&from.sa, &fromlen);
|
2009-12-02 10:02:30 -05:00
|
|
|
if(got_exit_signal)
|
|
|
|
break;
|
2016-04-03 16:35:43 -04:00
|
|
|
if(n < 0) {
|
2009-04-24 17:46:42 -04:00
|
|
|
logmsg("recvfrom");
|
2008-04-23 19:55:34 -04:00
|
|
|
result = 3;
|
|
|
|
break;
|
2005-09-15 16:32:59 -04:00
|
|
|
}
|
|
|
|
|
2008-04-23 19:55:34 -04:00
|
|
|
set_advisor_read_lock(SERVERLOGS_LOCK);
|
2009-11-26 05:15:08 -05:00
|
|
|
serverlogslocked = 1;
|
2008-04-23 19:55:34 -04:00
|
|
|
|
2010-11-19 13:20:38 -05:00
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
if(!use_ipv6) {
|
|
|
|
#endif
|
|
|
|
from.sa4.sin_family = AF_INET;
|
|
|
|
peer = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if(CURL_SOCKET_BAD == peer) {
|
|
|
|
logmsg("socket");
|
|
|
|
result = 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(connect(peer, &from.sa, sizeof(from.sa4)) < 0) {
|
|
|
|
logmsg("connect: fail");
|
|
|
|
result = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#ifdef ENABLE_IPV6
|
2005-09-15 16:32:59 -04:00
|
|
|
}
|
2010-11-19 13:20:38 -05:00
|
|
|
else {
|
|
|
|
from.sa6.sin6_family = AF_INET6;
|
|
|
|
peer = socket(AF_INET6, SOCK_DGRAM, 0);
|
|
|
|
if(CURL_SOCKET_BAD == peer) {
|
|
|
|
logmsg("socket");
|
|
|
|
result = 2;
|
|
|
|
break;
|
|
|
|
}
|
2013-04-06 13:00:42 -04:00
|
|
|
if(connect(peer, &from.sa, sizeof(from.sa6)) < 0) {
|
2010-11-19 13:20:38 -05:00
|
|
|
logmsg("connect: fail");
|
|
|
|
result = 1;
|
|
|
|
break;
|
|
|
|
}
|
2005-09-15 16:32:59 -04:00
|
|
|
}
|
2010-11-19 13:20:38 -05:00
|
|
|
#endif
|
|
|
|
|
2005-09-19 18:03:16 -04:00
|
|
|
maxtimeout = 5*TIMEOUT;
|
2005-09-15 16:32:59 -04:00
|
|
|
|
2010-11-19 13:20:38 -05:00
|
|
|
tp = &buf.hdr;
|
2005-09-15 16:32:59 -04:00
|
|
|
tp->th_opcode = ntohs(tp->th_opcode);
|
2016-04-03 16:35:43 -04:00
|
|
|
if(tp->th_opcode == opcode_RRQ || tp->th_opcode == opcode_WRQ) {
|
2005-09-15 16:32:59 -04:00
|
|
|
memset(&test, 0, sizeof(test));
|
2016-04-03 16:35:43 -04:00
|
|
|
if(do_tftp(&test, tp, n) < 0)
|
2005-09-19 18:03:16 -04:00
|
|
|
break;
|
2015-03-11 12:41:01 -04:00
|
|
|
free(test.buffer);
|
2005-09-15 16:32:59 -04:00
|
|
|
}
|
|
|
|
sclose(peer);
|
2009-12-02 10:02:30 -05:00
|
|
|
peer = CURL_SOCKET_BAD;
|
|
|
|
|
2009-12-03 15:37:49 -05:00
|
|
|
if(test.ofile > 0) {
|
|
|
|
close(test.ofile);
|
|
|
|
test.ofile = 0;
|
|
|
|
}
|
|
|
|
|
2009-12-02 10:02:30 -05:00
|
|
|
if(got_exit_signal)
|
|
|
|
break;
|
2008-04-23 19:55:34 -04:00
|
|
|
|
2009-11-26 05:15:08 -05:00
|
|
|
if(serverlogslocked) {
|
|
|
|
serverlogslocked = 0;
|
|
|
|
clear_advisor_read_lock(SERVERLOGS_LOCK);
|
|
|
|
}
|
2008-04-23 19:55:34 -04:00
|
|
|
|
2009-04-24 17:46:42 -04:00
|
|
|
logmsg("end of one transfer");
|
|
|
|
|
2009-11-23 11:11:50 -05:00
|
|
|
}
|
2008-04-23 19:55:34 -04:00
|
|
|
|
2009-12-02 10:02:30 -05:00
|
|
|
tftpd_cleanup:
|
|
|
|
|
2009-12-03 15:37:49 -05:00
|
|
|
if(test.ofile > 0)
|
|
|
|
close(test.ofile);
|
|
|
|
|
2009-12-02 10:02:30 -05:00
|
|
|
if((peer != sock) && (peer != CURL_SOCKET_BAD))
|
|
|
|
sclose(peer);
|
|
|
|
|
|
|
|
if(sock != CURL_SOCKET_BAD)
|
|
|
|
sclose(sock);
|
|
|
|
|
|
|
|
if(got_exit_signal)
|
|
|
|
logmsg("signalled to die");
|
|
|
|
|
|
|
|
if(wrotepidfile)
|
|
|
|
unlink(pidname);
|
|
|
|
|
2009-11-26 05:15:08 -05:00
|
|
|
if(serverlogslocked) {
|
|
|
|
serverlogslocked = 0;
|
|
|
|
clear_advisor_read_lock(SERVERLOGS_LOCK);
|
|
|
|
}
|
2008-04-23 19:55:34 -04:00
|
|
|
|
2009-12-02 10:02:30 -05:00
|
|
|
restore_signal_handlers();
|
|
|
|
|
|
|
|
if(got_exit_signal) {
|
|
|
|
logmsg("========> %s tftpd (port: %d pid: %ld) exits with signal (%d)",
|
|
|
|
ipv_inuse, (int)port, pid, exit_signal);
|
|
|
|
/*
|
|
|
|
* To properly set the return status of the process we
|
|
|
|
* must raise the same signal SIGINT or SIGTERM that we
|
|
|
|
* caught and let the old handler take care of it.
|
|
|
|
*/
|
|
|
|
raise(exit_signal);
|
|
|
|
}
|
|
|
|
|
|
|
|
logmsg("========> tftpd quits");
|
2008-04-23 19:55:34 -04:00
|
|
|
return result;
|
2005-09-15 16:32:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle initial connection protocol.
|
|
|
|
*/
|
2009-12-02 10:02:30 -05:00
|
|
|
static int do_tftp(struct testcase *test, struct tftphdr *tp, ssize_t size)
|
2005-09-15 16:32:59 -04:00
|
|
|
{
|
|
|
|
char *cp;
|
|
|
|
int first = 1, ecode;
|
|
|
|
struct formats *pf;
|
|
|
|
char *filename, *mode = NULL;
|
2007-10-24 09:03:08 -04:00
|
|
|
int error;
|
|
|
|
FILE *server;
|
2013-04-06 13:00:42 -04:00
|
|
|
#ifdef USE_WINSOCK
|
|
|
|
DWORD recvtimeout, recvtimeoutbak;
|
|
|
|
#endif
|
2017-05-04 14:19:10 -04:00
|
|
|
const char *option = "mode"; /* mode is implicit */
|
2015-11-06 04:52:19 -05:00
|
|
|
int toggle = 1;
|
2007-10-24 09:03:08 -04:00
|
|
|
|
|
|
|
/* Open request dump file. */
|
|
|
|
server = fopen(REQUEST_DUMP, "ab");
|
|
|
|
if(!server) {
|
2013-01-09 09:10:23 -05:00
|
|
|
error = errno;
|
2007-10-24 09:03:08 -04:00
|
|
|
logmsg("fopen() failed with error: %d %s", error, strerror(error));
|
|
|
|
logmsg("Error opening file: %s", REQUEST_DUMP);
|
|
|
|
return -1;
|
|
|
|
}
|
2005-09-15 16:32:59 -04:00
|
|
|
|
|
|
|
/* store input protocol */
|
2007-10-24 09:03:08 -04:00
|
|
|
fprintf(server, "opcode: %x\n", tp->th_opcode);
|
2005-09-15 16:32:59 -04:00
|
|
|
|
2005-09-16 06:50:43 -04:00
|
|
|
cp = (char *)&tp->th_stuff;
|
|
|
|
filename = cp;
|
2015-11-06 04:52:19 -05:00
|
|
|
do {
|
|
|
|
bool endofit = true;
|
2016-04-03 16:35:43 -04:00
|
|
|
while(cp < &buf.storage[size]) {
|
|
|
|
if(*cp == '\0') {
|
2015-11-06 04:52:19 -05:00
|
|
|
endofit = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cp++;
|
|
|
|
}
|
|
|
|
if(endofit)
|
|
|
|
/* no more options */
|
2005-09-15 16:32:59 -04:00
|
|
|
break;
|
2015-11-06 04:52:19 -05:00
|
|
|
|
|
|
|
/* before increasing pointer, make sure it is still within the legal
|
|
|
|
space */
|
2017-09-09 17:55:08 -04:00
|
|
|
if((cp + 1) < &buf.storage[size]) {
|
2015-11-06 04:52:19 -05:00
|
|
|
++cp;
|
|
|
|
if(first) {
|
|
|
|
/* store the mode since we need it later */
|
|
|
|
mode = cp;
|
|
|
|
first = 0;
|
|
|
|
}
|
|
|
|
if(toggle)
|
|
|
|
/* name/value pair: */
|
|
|
|
fprintf(server, "%s: %s\n", option, cp);
|
|
|
|
else {
|
|
|
|
/* store the name pointer */
|
|
|
|
option = cp;
|
|
|
|
}
|
|
|
|
toggle ^= 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
/* No more options */
|
|
|
|
break;
|
|
|
|
} while(1);
|
|
|
|
|
2016-04-03 16:35:43 -04:00
|
|
|
if(*cp) {
|
2005-09-15 16:32:59 -04:00
|
|
|
nak(EBADOP);
|
2007-10-24 09:03:08 -04:00
|
|
|
fclose(server);
|
2005-09-15 16:32:59 -04:00
|
|
|
return 3;
|
|
|
|
}
|
2015-11-06 04:52:19 -05:00
|
|
|
|
2005-09-15 16:32:59 -04:00
|
|
|
/* store input protocol */
|
2007-10-24 09:03:08 -04:00
|
|
|
fprintf(server, "filename: %s\n", filename);
|
2005-09-15 16:32:59 -04:00
|
|
|
|
2016-04-03 16:35:43 -04:00
|
|
|
for(cp = mode; cp && *cp; cp++)
|
2007-11-15 08:20:18 -05:00
|
|
|
if(ISUPPER(*cp))
|
2007-02-02 12:16:06 -05:00
|
|
|
*cp = (char)tolower((int)*cp);
|
2005-09-15 16:32:59 -04:00
|
|
|
|
|
|
|
/* store input protocol */
|
2007-10-24 09:03:08 -04:00
|
|
|
fclose(server);
|
2005-09-15 16:32:59 -04:00
|
|
|
|
2016-04-03 16:35:43 -04:00
|
|
|
for(pf = formata; pf->f_mode; pf++)
|
|
|
|
if(strcmp(pf->f_mode, mode) == 0)
|
2005-09-15 16:32:59 -04:00
|
|
|
break;
|
2016-04-03 16:35:43 -04:00
|
|
|
if(!pf->f_mode) {
|
2005-09-15 16:32:59 -04:00
|
|
|
nak(EBADOP);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
ecode = validate_access(test, filename, tp->th_opcode);
|
2016-04-03 16:35:43 -04:00
|
|
|
if(ecode) {
|
2005-09-15 16:32:59 -04:00
|
|
|
nak(ecode);
|
|
|
|
return 1;
|
|
|
|
}
|
2013-04-06 13:00:42 -04:00
|
|
|
|
|
|
|
#ifdef USE_WINSOCK
|
|
|
|
recvtimeout = sizeof(recvtimeoutbak);
|
|
|
|
getsockopt(peer, SOL_SOCKET, SO_RCVTIMEO,
|
2016-11-23 02:30:18 -05:00
|
|
|
(char *)&recvtimeoutbak, (int *)&recvtimeout);
|
2013-04-06 13:00:42 -04:00
|
|
|
recvtimeout = TIMEOUT*1000;
|
|
|
|
setsockopt(peer, SOL_SOCKET, SO_RCVTIMEO,
|
2016-11-23 02:30:18 -05:00
|
|
|
(const char *)&recvtimeout, sizeof(recvtimeout));
|
2013-04-06 13:00:42 -04:00
|
|
|
#endif
|
|
|
|
|
2016-04-03 16:35:43 -04:00
|
|
|
if(tp->th_opcode == opcode_WRQ)
|
2005-09-16 06:52:38 -04:00
|
|
|
recvtftp(test, pf);
|
2005-09-15 16:32:59 -04:00
|
|
|
else
|
2005-09-16 06:52:38 -04:00
|
|
|
sendtftp(test, pf);
|
2005-09-19 18:03:16 -04:00
|
|
|
|
2013-04-06 13:00:42 -04:00
|
|
|
#ifdef USE_WINSOCK
|
|
|
|
recvtimeout = recvtimeoutbak;
|
|
|
|
setsockopt(peer, SOL_SOCKET, SO_RCVTIMEO,
|
2016-11-23 02:30:18 -05:00
|
|
|
(const char *)&recvtimeout, sizeof(recvtimeout));
|
2013-04-06 13:00:42 -04:00
|
|
|
#endif
|
|
|
|
|
2005-09-15 16:32:59 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-22 13:23:08 -04:00
|
|
|
/* Based on the testno, parse the correct server commands. */
|
|
|
|
static int parse_servercmd(struct testcase *req)
|
|
|
|
{
|
|
|
|
FILE *stream;
|
|
|
|
char *filename;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
filename = test2file(req->testno);
|
|
|
|
|
2017-09-09 17:09:06 -04:00
|
|
|
stream = fopen(filename, "rb");
|
2013-08-22 13:23:08 -04:00
|
|
|
if(!stream) {
|
|
|
|
error = errno;
|
|
|
|
logmsg("fopen() failed with error: %d %s", error, strerror(error));
|
|
|
|
logmsg(" [1] Error opening file: %s", filename);
|
|
|
|
logmsg(" Couldn't open test file %ld", req->testno);
|
|
|
|
return 1; /* done */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
char *orgcmd = NULL;
|
|
|
|
char *cmd = NULL;
|
|
|
|
size_t cmdsize = 0;
|
2017-09-09 17:09:06 -04:00
|
|
|
int num = 0;
|
2013-08-22 13:23:08 -04:00
|
|
|
|
|
|
|
/* get the custom server control "commands" */
|
|
|
|
error = getpart(&orgcmd, &cmdsize, "reply", "servercmd", stream);
|
|
|
|
fclose(stream);
|
|
|
|
if(error) {
|
|
|
|
logmsg("getpart() failed with error: %d", error);
|
|
|
|
return 1; /* done */
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd = orgcmd;
|
|
|
|
while(cmd && cmdsize) {
|
|
|
|
char *check;
|
|
|
|
if(1 == sscanf(cmd, "writedelay: %d", &num)) {
|
|
|
|
logmsg("instructed to delay %d secs between packets", num);
|
|
|
|
req->writedelay = num;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
logmsg("Unknown <servercmd> instruction found: %s", cmd);
|
|
|
|
}
|
|
|
|
/* try to deal with CRLF or just LF */
|
|
|
|
check = strchr(cmd, '\r');
|
|
|
|
if(!check)
|
|
|
|
check = strchr(cmd, '\n');
|
|
|
|
|
|
|
|
if(check) {
|
|
|
|
/* get to the letter following the newline */
|
|
|
|
while((*check == '\r') || (*check == '\n'))
|
|
|
|
check++;
|
|
|
|
|
|
|
|
if(!*check)
|
|
|
|
/* if we reached a zero, get out */
|
|
|
|
break;
|
|
|
|
cmd = check;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2015-03-11 12:41:01 -04:00
|
|
|
free(orgcmd);
|
2013-08-22 13:23:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0; /* OK! */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-09-15 16:32:59 -04:00
|
|
|
/*
|
|
|
|
* Validate file access.
|
|
|
|
*/
|
|
|
|
static int validate_access(struct testcase *test,
|
|
|
|
const char *filename, int mode)
|
|
|
|
{
|
|
|
|
char *ptr;
|
2007-10-24 18:48:23 -04:00
|
|
|
long testno, partno;
|
2007-02-18 21:03:58 -05:00
|
|
|
int error;
|
2007-10-24 18:48:23 -04:00
|
|
|
char partbuf[80]="data";
|
2005-09-15 16:32:59 -04:00
|
|
|
|
|
|
|
logmsg("trying to get file: %s mode %x", filename, mode);
|
|
|
|
|
2008-02-06 12:35:17 -05:00
|
|
|
if(!strncmp("verifiedserver", filename, 14)) {
|
2005-09-15 16:32:59 -04:00
|
|
|
char weare[128];
|
2016-04-03 16:35:43 -04:00
|
|
|
size_t count = snprintf(weare, sizeof(weare),
|
|
|
|
"WE ROOLZ: %ld\r\n", (long)getpid());
|
2005-09-15 16:32:59 -04:00
|
|
|
|
|
|
|
logmsg("Are-we-friendly question received");
|
|
|
|
test->buffer = strdup(weare);
|
|
|
|
test->rptr = test->buffer; /* set read pointer */
|
|
|
|
test->bufsize = count; /* set total count */
|
|
|
|
test->rcount = count; /* set data left to read */
|
|
|
|
return 0; /* fine */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find the last slash */
|
|
|
|
ptr = strrchr(filename, '/');
|
|
|
|
|
|
|
|
if(ptr) {
|
|
|
|
char *file;
|
|
|
|
|
|
|
|
ptr++; /* skip the slash */
|
|
|
|
|
|
|
|
/* skip all non-numericals following the slash */
|
2006-10-23 15:14:54 -04:00
|
|
|
while(*ptr && !ISDIGIT(*ptr))
|
2005-09-15 16:32:59 -04:00
|
|
|
ptr++;
|
|
|
|
|
|
|
|
/* get the number */
|
|
|
|
testno = strtol(ptr, &ptr, 10);
|
|
|
|
|
2007-10-24 18:48:23 -04:00
|
|
|
if(testno > 10000) {
|
|
|
|
partno = testno % 10000;
|
|
|
|
testno /= 10000;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
partno = 0;
|
|
|
|
|
|
|
|
|
|
|
|
logmsg("requested test number %ld part %ld", testno, partno);
|
2005-09-15 16:32:59 -04:00
|
|
|
|
2013-08-22 13:23:08 -04:00
|
|
|
test->testno = testno;
|
|
|
|
|
|
|
|
(void)parse_servercmd(test);
|
2005-09-15 16:32:59 -04:00
|
|
|
|
|
|
|
file = test2file(testno);
|
|
|
|
|
2007-10-24 18:48:23 -04:00
|
|
|
if(0 != partno)
|
2016-04-03 16:35:43 -04:00
|
|
|
snprintf(partbuf, sizeof(partbuf), "data%ld", partno);
|
2007-10-24 18:48:23 -04:00
|
|
|
|
2005-09-15 16:32:59 -04:00
|
|
|
if(file) {
|
2017-09-09 17:09:06 -04:00
|
|
|
FILE *stream = fopen(file, "rb");
|
2005-09-15 16:32:59 -04:00
|
|
|
if(!stream) {
|
2013-01-09 09:10:23 -05:00
|
|
|
error = errno;
|
2007-02-18 21:03:58 -05:00
|
|
|
logmsg("fopen() failed with error: %d %s", error, strerror(error));
|
|
|
|
logmsg("Error opening file: %s", file);
|
2005-09-15 16:32:59 -04:00
|
|
|
logmsg("Couldn't open test file: %s", file);
|
|
|
|
return EACCESS;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
size_t count;
|
2010-02-14 08:14:17 -05:00
|
|
|
error = getpart(&test->buffer, &count, "reply", partbuf, stream);
|
2005-09-15 16:32:59 -04:00
|
|
|
fclose(stream);
|
2010-02-14 08:14:17 -05:00
|
|
|
if(error) {
|
|
|
|
logmsg("getpart() failed with error: %d", error);
|
|
|
|
return EACCESS;
|
|
|
|
}
|
2005-09-15 16:32:59 -04:00
|
|
|
if(test->buffer) {
|
|
|
|
test->rptr = test->buffer; /* set read pointer */
|
|
|
|
test->bufsize = count; /* set total count */
|
|
|
|
test->rcount = count; /* set data left to read */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return EACCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return EACCESS;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
logmsg("no slash found in path");
|
|
|
|
return EACCESS; /* failure */
|
|
|
|
}
|
|
|
|
|
2009-04-24 17:46:42 -04:00
|
|
|
logmsg("file opened and all is good");
|
2005-09-15 16:32:59 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Send the requested file.
|
|
|
|
*/
|
2005-09-16 06:52:38 -04:00
|
|
|
static void sendtftp(struct testcase *test, struct formats *pf)
|
2005-09-15 16:32:59 -04:00
|
|
|
{
|
2006-07-28 14:01:23 -04:00
|
|
|
int size;
|
|
|
|
ssize_t n;
|
2016-11-12 06:33:10 -05:00
|
|
|
/* These are volatile to live through a siglongjmp */
|
2014-05-29 03:24:12 -04:00
|
|
|
volatile unsigned short sendblock; /* block count */
|
2016-11-12 06:33:10 -05:00
|
|
|
struct tftphdr * volatile sdp = r_init(); /* data buffer */
|
|
|
|
struct tftphdr * const sap = &ackbuf.hdr; /* ack buffer */
|
2013-08-22 07:44:37 -04:00
|
|
|
|
2008-10-14 13:35:25 -04:00
|
|
|
sendblock = 1;
|
2006-01-03 17:19:15 -05:00
|
|
|
#if defined(HAVE_ALARM) && defined(SIGALRM)
|
2005-09-15 16:32:59 -04:00
|
|
|
mysignal(SIGALRM, timer);
|
2006-01-03 17:19:15 -05:00
|
|
|
#endif
|
2005-09-15 16:32:59 -04:00
|
|
|
do {
|
2016-11-12 06:33:10 -05:00
|
|
|
size = readit(test, (struct tftphdr **)&sdp, pf->f_convert);
|
2016-04-03 16:35:43 -04:00
|
|
|
if(size < 0) {
|
2013-01-09 09:10:23 -05:00
|
|
|
nak(errno + 100);
|
2005-09-19 18:03:16 -04:00
|
|
|
return;
|
2005-09-15 16:32:59 -04:00
|
|
|
}
|
2011-04-06 01:30:09 -04:00
|
|
|
sdp->th_opcode = htons((unsigned short)opcode_DATA);
|
|
|
|
sdp->th_block = htons(sendblock);
|
2005-09-15 16:32:59 -04:00
|
|
|
timeout = 0;
|
2006-01-03 17:44:58 -05:00
|
|
|
#ifdef HAVE_SIGSETJMP
|
2005-09-15 16:32:59 -04:00
|
|
|
(void) sigsetjmp(timeoutbuf, 1);
|
2006-01-03 17:19:15 -05:00
|
|
|
#endif
|
2013-08-22 13:23:08 -04:00
|
|
|
if(test->writedelay) {
|
|
|
|
logmsg("Pausing %d seconds before %d bytes", test->writedelay,
|
|
|
|
size);
|
|
|
|
wait_ms(1000*test->writedelay);
|
|
|
|
}
|
|
|
|
|
2005-09-15 16:32:59 -04:00
|
|
|
send_data:
|
2016-04-03 16:35:43 -04:00
|
|
|
if(swrite(peer, sdp, size + 4) != size + 4) {
|
2009-04-24 17:46:42 -04:00
|
|
|
logmsg("write");
|
2005-09-19 18:03:16 -04:00
|
|
|
return;
|
2005-09-15 16:32:59 -04:00
|
|
|
}
|
|
|
|
read_ahead(test, pf->f_convert);
|
2016-04-03 16:35:43 -04:00
|
|
|
for(;;) {
|
2006-01-03 17:44:58 -05:00
|
|
|
#ifdef HAVE_ALARM
|
2005-09-15 16:32:59 -04:00
|
|
|
alarm(rexmtval); /* read the ack */
|
2006-01-03 17:19:15 -05:00
|
|
|
#endif
|
2010-11-19 13:20:38 -05:00
|
|
|
n = sread(peer, &ackbuf.storage[0], sizeof(ackbuf.storage));
|
2006-01-03 17:44:58 -05:00
|
|
|
#ifdef HAVE_ALARM
|
2005-09-15 16:32:59 -04:00
|
|
|
alarm(0);
|
2006-01-03 17:44:58 -05:00
|
|
|
#endif
|
2009-12-02 10:02:30 -05:00
|
|
|
if(got_exit_signal)
|
|
|
|
return;
|
2016-04-03 16:35:43 -04:00
|
|
|
if(n < 0) {
|
2009-04-24 17:46:42 -04:00
|
|
|
logmsg("read: fail");
|
2005-09-19 18:03:16 -04:00
|
|
|
return;
|
2005-09-15 16:32:59 -04:00
|
|
|
}
|
2011-04-06 01:30:09 -04:00
|
|
|
sap->th_opcode = ntohs((unsigned short)sap->th_opcode);
|
|
|
|
sap->th_block = ntohs(sap->th_block);
|
2005-09-15 16:32:59 -04:00
|
|
|
|
2016-04-03 16:35:43 -04:00
|
|
|
if(sap->th_opcode == opcode_ERROR) {
|
2005-09-19 18:03:16 -04:00
|
|
|
logmsg("got ERROR");
|
|
|
|
return;
|
|
|
|
}
|
2005-09-15 16:32:59 -04:00
|
|
|
|
2016-04-03 16:35:43 -04:00
|
|
|
if(sap->th_opcode == opcode_ACK) {
|
|
|
|
if(sap->th_block == sendblock) {
|
2005-09-15 16:32:59 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Re-synchronize with the other side */
|
|
|
|
(void) synchnet(peer);
|
2016-04-03 16:35:43 -04:00
|
|
|
if(sap->th_block == (sendblock-1)) {
|
2005-09-15 16:32:59 -04:00
|
|
|
goto send_data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2008-10-14 13:35:25 -04:00
|
|
|
sendblock++;
|
2016-04-03 16:35:43 -04:00
|
|
|
} while(size == SEGSIZE);
|
2005-09-15 16:32:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Receive a file.
|
|
|
|
*/
|
2005-09-16 06:52:38 -04:00
|
|
|
static void recvtftp(struct testcase *test, struct formats *pf)
|
2005-09-15 16:32:59 -04:00
|
|
|
{
|
2006-07-29 05:15:03 -04:00
|
|
|
ssize_t n, size;
|
2014-05-29 03:24:12 -04:00
|
|
|
/* These are volatile to live through a siglongjmp */
|
|
|
|
volatile unsigned short recvblock; /* block count */
|
|
|
|
struct tftphdr * volatile rdp; /* data buffer */
|
2013-08-22 07:44:37 -04:00
|
|
|
struct tftphdr *rap; /* ack buffer */
|
|
|
|
|
2008-10-14 13:35:25 -04:00
|
|
|
recvblock = 0;
|
2014-05-29 03:24:12 -04:00
|
|
|
rdp = w_init();
|
2006-01-03 17:19:15 -05:00
|
|
|
#if defined(HAVE_ALARM) && defined(SIGALRM)
|
2005-09-15 16:32:59 -04:00
|
|
|
mysignal(SIGALRM, timer);
|
2006-01-03 17:19:15 -05:00
|
|
|
#endif
|
2010-11-19 13:20:38 -05:00
|
|
|
rap = &ackbuf.hdr;
|
2005-09-15 16:32:59 -04:00
|
|
|
do {
|
|
|
|
timeout = 0;
|
2011-04-06 01:30:09 -04:00
|
|
|
rap->th_opcode = htons((unsigned short)opcode_ACK);
|
|
|
|
rap->th_block = htons(recvblock);
|
2008-10-14 13:35:25 -04:00
|
|
|
recvblock++;
|
2006-01-03 17:19:15 -05:00
|
|
|
#ifdef HAVE_SIGSETJMP
|
2005-09-15 16:32:59 -04:00
|
|
|
(void) sigsetjmp(timeoutbuf, 1);
|
2006-01-03 17:19:15 -05:00
|
|
|
#endif
|
2005-09-15 16:32:59 -04:00
|
|
|
send_ack:
|
2016-04-03 16:35:43 -04:00
|
|
|
if(swrite(peer, &ackbuf.storage[0], 4) != 4) {
|
2005-09-15 16:32:59 -04:00
|
|
|
logmsg("write: fail\n");
|
|
|
|
goto abort;
|
|
|
|
}
|
|
|
|
write_behind(test, pf->f_convert);
|
2016-04-03 16:35:43 -04:00
|
|
|
for(;;) {
|
2006-01-03 17:44:58 -05:00
|
|
|
#ifdef HAVE_ALARM
|
2005-09-15 16:32:59 -04:00
|
|
|
alarm(rexmtval);
|
2006-01-03 17:44:58 -05:00
|
|
|
#endif
|
2008-10-14 13:35:25 -04:00
|
|
|
n = sread(peer, rdp, PKTSIZE);
|
2006-01-03 17:44:58 -05:00
|
|
|
#ifdef HAVE_ALARM
|
2005-09-15 16:32:59 -04:00
|
|
|
alarm(0);
|
2006-01-03 17:19:15 -05:00
|
|
|
#endif
|
2009-12-02 10:02:30 -05:00
|
|
|
if(got_exit_signal)
|
|
|
|
goto abort;
|
2016-04-03 16:35:43 -04:00
|
|
|
if(n < 0) { /* really? */
|
2005-09-15 16:32:59 -04:00
|
|
|
logmsg("read: fail\n");
|
|
|
|
goto abort;
|
|
|
|
}
|
2011-04-06 01:30:09 -04:00
|
|
|
rdp->th_opcode = ntohs((unsigned short)rdp->th_opcode);
|
|
|
|
rdp->th_block = ntohs(rdp->th_block);
|
2016-04-03 16:35:43 -04:00
|
|
|
if(rdp->th_opcode == opcode_ERROR)
|
2005-09-15 16:32:59 -04:00
|
|
|
goto abort;
|
2016-04-03 16:35:43 -04:00
|
|
|
if(rdp->th_opcode == opcode_DATA) {
|
|
|
|
if(rdp->th_block == recvblock) {
|
2005-09-15 16:32:59 -04:00
|
|
|
break; /* normal */
|
|
|
|
}
|
|
|
|
/* Re-synchronize with the other side */
|
|
|
|
(void) synchnet(peer);
|
2016-04-03 16:35:43 -04:00
|
|
|
if(rdp->th_block == (recvblock-1))
|
2005-09-15 16:32:59 -04:00
|
|
|
goto send_ack; /* rexmit */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-14 13:35:25 -04:00
|
|
|
size = writeit(test, &rdp, (int)(n - 4), pf->f_convert);
|
2016-04-03 16:35:43 -04:00
|
|
|
if(size != (n-4)) { /* ahem */
|
|
|
|
if(size < 0)
|
2013-01-09 09:10:23 -05:00
|
|
|
nak(errno + 100);
|
2005-09-15 16:32:59 -04:00
|
|
|
else
|
|
|
|
nak(ENOSPACE);
|
|
|
|
goto abort;
|
|
|
|
}
|
2016-04-03 16:35:43 -04:00
|
|
|
} while(size == SEGSIZE);
|
2005-09-15 16:32:59 -04:00
|
|
|
write_behind(test, pf->f_convert);
|
|
|
|
|
2016-04-03 16:35:43 -04:00
|
|
|
rap->th_opcode = htons((unsigned short)opcode_ACK); /* send the "final"
|
|
|
|
ack */
|
2011-04-06 01:30:09 -04:00
|
|
|
rap->th_block = htons(recvblock);
|
2010-11-19 13:20:38 -05:00
|
|
|
(void) swrite(peer, &ackbuf.storage[0], 4);
|
2006-01-03 17:19:15 -05:00
|
|
|
#if defined(HAVE_ALARM) && defined(SIGALRM)
|
2007-01-15 16:03:53 -05:00
|
|
|
mysignal(SIGALRM, justtimeout); /* just abort read on timeout */
|
2005-09-15 16:32:59 -04:00
|
|
|
alarm(rexmtval);
|
2006-01-03 17:44:58 -05:00
|
|
|
#endif
|
2010-11-19 13:20:38 -05:00
|
|
|
/* normally times out and quits */
|
|
|
|
n = sread(peer, &buf.storage[0], sizeof(buf.storage));
|
2006-01-03 17:44:58 -05:00
|
|
|
#ifdef HAVE_ALARM
|
2005-09-15 16:32:59 -04:00
|
|
|
alarm(0);
|
2006-01-03 17:44:58 -05:00
|
|
|
#endif
|
2009-12-02 10:02:30 -05:00
|
|
|
if(got_exit_signal)
|
|
|
|
goto abort;
|
2016-04-03 16:35:43 -04:00
|
|
|
if(n >= 4 && /* if read some data */
|
|
|
|
rdp->th_opcode == opcode_DATA && /* and got a data block */
|
|
|
|
recvblock == rdp->th_block) { /* then my last ack was lost */
|
2010-11-19 13:20:38 -05:00
|
|
|
(void) swrite(peer, &ackbuf.storage[0], 4); /* resend final ack */
|
2005-09-15 16:32:59 -04:00
|
|
|
}
|
|
|
|
abort:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Send a nak packet (error message). Error code passed in is one of the
|
2014-12-26 15:45:21 -05:00
|
|
|
* standard TFTP codes, or a Unix errno offset by 100.
|
2005-09-15 16:32:59 -04:00
|
|
|
*/
|
|
|
|
static void nak(int error)
|
|
|
|
{
|
|
|
|
struct tftphdr *tp;
|
|
|
|
int length;
|
|
|
|
struct errmsg *pe;
|
|
|
|
|
2010-11-19 13:20:38 -05:00
|
|
|
tp = &buf.hdr;
|
2011-04-06 01:30:09 -04:00
|
|
|
tp->th_opcode = htons((unsigned short)opcode_ERROR);
|
|
|
|
tp->th_code = htons((unsigned short)error);
|
2016-04-03 16:35:43 -04:00
|
|
|
for(pe = errmsgs; pe->e_code >= 0; pe++)
|
|
|
|
if(pe->e_code == error)
|
2005-09-15 16:32:59 -04:00
|
|
|
break;
|
2016-04-03 16:35:43 -04:00
|
|
|
if(pe->e_code < 0) {
|
2005-09-15 16:32:59 -04:00
|
|
|
pe->e_msg = strerror(error - 100);
|
|
|
|
tp->th_code = EUNDEF; /* set 'undef' errorcode */
|
|
|
|
}
|
2005-12-05 09:10:48 -05:00
|
|
|
length = (int)strlen(pe->e_msg);
|
2010-12-17 13:07:45 -05:00
|
|
|
|
|
|
|
/* we use memcpy() instead of strcpy() in order to avoid buffer overflow
|
|
|
|
* report from glibc with FORTIFY_SOURCE */
|
|
|
|
memcpy(tp->th_msg, pe->e_msg, length + 1);
|
2005-09-15 16:32:59 -04:00
|
|
|
length += 5;
|
2016-04-03 16:35:43 -04:00
|
|
|
if(swrite(peer, &buf.storage[0], length) != length)
|
2005-09-15 16:32:59 -04:00
|
|
|
logmsg("nak: fail\n");
|
|
|
|
}
|