2005-09-16 04:32:59 +08: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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1983 Regents of the University of California.
|
|
|
|
* 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 23:32:06 +08:00
|
|
|
#include "server_setup.h"
|
2008-02-28 08:55:06 +08:00
|
|
|
|
2005-09-16 05:50:50 +08:00
|
|
|
#ifdef HAVE_SYS_IOCTL_H
|
2005-09-16 04:32:59 +08:00
|
|
|
#include <sys/ioctl.h>
|
2005-09-16 05:50:50 +08:00
|
|
|
#endif
|
2008-02-28 08:55:06 +08:00
|
|
|
#ifdef HAVE_SIGNAL_H
|
2005-09-16 04:32:59 +08:00
|
|
|
#include <signal.h>
|
2008-02-28 08:55:06 +08:00
|
|
|
#endif
|
2005-09-16 05:50:50 +08:00
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
2005-09-16 04:32:59 +08:00
|
|
|
#include <sys/socket.h>
|
2005-09-16 05:50:50 +08:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETINET_IN_H
|
2005-09-16 04:32:59 +08:00
|
|
|
#include <netinet/in.h>
|
2005-09-16 05:50:50 +08:00
|
|
|
#endif
|
2009-04-28 07:59:41 +08:00
|
|
|
#ifdef HAVE_ARPA_INET_H
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
2005-09-16 05:50:50 +08:00
|
|
|
#ifdef HAVE_ARPA_TFTP_H
|
2005-09-16 04:32:59 +08:00
|
|
|
#include <arpa/tftp.h>
|
2006-01-04 06:19:15 +08:00
|
|
|
#else
|
|
|
|
#include "tftp.h"
|
2005-09-16 05:50:50 +08:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETDB_H
|
2005-09-16 04:32:59 +08:00
|
|
|
#include <netdb.h>
|
2005-09-16 05:50:50 +08:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_FILIO_H
|
|
|
|
/* FIONREAD on Solaris 7 */
|
|
|
|
#include <sys/filio.h>
|
|
|
|
#endif
|
2005-09-16 04:32:59 +08:00
|
|
|
|
|
|
|
#include <setjmp.h>
|
2008-02-28 08:55:06 +08:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2005-09-16 04:32:59 +08:00
|
|
|
#include <unistd.h>
|
2008-02-28 08:55:06 +08:00
|
|
|
#endif
|
2006-01-04 06:19:15 +08:00
|
|
|
#ifdef HAVE_PWD_H
|
2005-09-16 04:32:59 +08:00
|
|
|
#include <pwd.h>
|
2006-01-04 06:19:15 +08:00
|
|
|
#endif
|
2005-09-16 04:32:59 +08:00
|
|
|
|
2005-09-16 13:49:53 +08: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-16 04:32:59 +08:00
|
|
|
#include "getpart.h"
|
|
|
|
#include "util.h"
|
2010-11-20 02:20:38 +08:00
|
|
|
#include "server_sockaddr.h"
|
2005-09-16 04:32:59 +08:00
|
|
|
|
2005-09-20 06:03:16 +08:00
|
|
|
/* include memdebug.h last */
|
|
|
|
#include "memdebug.h"
|
|
|
|
|
2009-12-02 23:02:30 +08:00
|
|
|
/*****************************************************************************
|
|
|
|
* STRUCT DECLARATIONS AND DEFINES *
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2010-01-23 21:51:53 +08:00
|
|
|
#ifndef PKTSIZE
|
2009-12-02 23:02:30 +08:00
|
|
|
#define PKTSIZE (SEGSIZE + 4) /* SEGSIZE defined in arpa/tftp.h */
|
2010-01-23 21:51:53 +08:00
|
|
|
#endif
|
2008-09-26 19:21:22 +08:00
|
|
|
|
2005-09-16 04:32:59 +08: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 */
|
|
|
|
long num; /* test case number */
|
|
|
|
int ofile; /* file descriptor for output file when uploading to us */
|
|
|
|
};
|
|
|
|
|
2009-12-02 23:02:30 +08:00
|
|
|
struct formats {
|
|
|
|
const char *f_mode;
|
|
|
|
int f_convert;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct errmsg {
|
|
|
|
int e_code;
|
|
|
|
const char *e_msg;
|
|
|
|
};
|
|
|
|
|
2010-11-20 02:20:38 +08:00
|
|
|
typedef union {
|
|
|
|
struct tftphdr hdr;
|
|
|
|
char storage[PKTSIZE];
|
|
|
|
} tftphdr_storage_t;
|
|
|
|
|
2009-12-02 23:02:30 +08: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-20 02:20:38 +08:00
|
|
|
tftphdr_storage_t buf; /* room for data packet */
|
2009-12-02 23:02:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#define BF_ALLOC -3 /* alloc'd but not yet filled */
|
|
|
|
#define BF_FREE -2 /* free */
|
2005-09-16 04:32:59 +08:00
|
|
|
|
2008-10-23 22:34:08 +08:00
|
|
|
#define opcode_RRQ 1
|
|
|
|
#define opcode_WRQ 2
|
|
|
|
#define opcode_DATA 3
|
|
|
|
#define opcode_ACK 4
|
|
|
|
#define opcode_ERROR 5
|
2005-09-16 04:32:59 +08:00
|
|
|
|
2009-12-02 23:02:30 +08:00
|
|
|
#define TIMEOUT 5
|
2005-09-16 04:32:59 +08:00
|
|
|
|
2009-12-02 23:02:30 +08:00
|
|
|
#undef MIN
|
|
|
|
#define MIN(x,y) ((x)<(y)?(x):(y))
|
2005-09-16 04:32:59 +08:00
|
|
|
|
2009-12-02 23:02:30 +08: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 20:33:02 +08:00
|
|
|
};
|
2009-12-02 23:02:30 +08:00
|
|
|
|
2008-09-20 20:33:02 +08:00
|
|
|
static struct formats formata[] = {
|
|
|
|
{ "netascii", 1 },
|
|
|
|
{ "octet", 0 },
|
|
|
|
{ NULL, 0 }
|
|
|
|
};
|
|
|
|
|
2009-12-02 23:02:30 +08: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-16 04:32:59 +08:00
|
|
|
|
2009-12-02 23:02:30 +08: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-16 04:32:59 +08:00
|
|
|
|
2010-11-20 02:20:38 +08:00
|
|
|
static tftphdr_storage_t buf;
|
|
|
|
static tftphdr_storage_t ackbuf;
|
2009-12-02 23:02:30 +08:00
|
|
|
|
2010-11-20 02:20:38 +08:00
|
|
|
static srvr_sockaddr_union_t from;
|
2009-05-02 10:37:32 +08:00
|
|
|
static curl_socklen_t fromlen;
|
2005-09-16 04:32:59 +08:00
|
|
|
|
2009-12-02 23:02:30 +08:00
|
|
|
static curl_socket_t peer = CURL_SOCKET_BAD;
|
2005-09-16 04:32:59 +08:00
|
|
|
|
2009-12-02 23:02:30 +08:00
|
|
|
static int timeout;
|
|
|
|
static int maxtimeout = 5 * TIMEOUT;
|
2005-09-16 04:32:59 +08:00
|
|
|
|
2009-12-02 23:02:30 +08:00
|
|
|
static unsigned short sendblock; /* block count used by sendtftp() */
|
|
|
|
static struct tftphdr *sdp; /* data buffer used by sendtftp() */
|
|
|
|
static struct tftphdr *sap; /* ack buffer used by sendtftp() */
|
2005-09-16 04:32:59 +08:00
|
|
|
|
2009-12-02 23:02:30 +08:00
|
|
|
static unsigned short recvblock; /* block count used by recvtftp() */
|
|
|
|
static struct tftphdr *rdp; /* data buffer used by recvtftp() */
|
|
|
|
static struct tftphdr *rap; /* ack buffer used by recvtftp() */
|
|
|
|
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
static bool use_ipv6 = FALSE;
|
|
|
|
#endif
|
|
|
|
static const char *ipv_inuse = "IPv4";
|
|
|
|
|
|
|
|
const char *serverlogfile = DEFAULT_LOGFILE;
|
|
|
|
static char *pidname= (char *)".tftpd.pid";
|
|
|
|
static int serverlogslocked = 0;
|
|
|
|
static int wrotepidfile = 0;
|
|
|
|
|
|
|
|
#ifdef HAVE_SIGSETJMP
|
|
|
|
static sigjmp_buf timeoutbuf;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(HAVE_ALARM) && defined(SIGALRM)
|
|
|
|
static int rexmtval = TIMEOUT;
|
|
|
|
#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
|
|
|
|
|
|
|
|
/* 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-16 04:32:59 +08:00
|
|
|
|
|
|
|
static struct tftphdr *rw_init(int);
|
|
|
|
|
2009-12-02 23:02:30 +08:00
|
|
|
static struct tftphdr *w_init(void);
|
|
|
|
|
|
|
|
static struct tftphdr *r_init(void);
|
|
|
|
|
|
|
|
static int readit(struct testcase *test,
|
|
|
|
struct tftphdr **dpp,
|
|
|
|
int convert);
|
|
|
|
|
|
|
|
static int writeit(struct testcase *test,
|
|
|
|
struct tftphdr **dpp,
|
|
|
|
int ct,
|
|
|
|
int convert);
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
int old_errno = ERRNO;
|
|
|
|
if(got_exit_signal == 0) {
|
|
|
|
got_exit_signal = 1;
|
|
|
|
exit_signal = signum;
|
|
|
|
}
|
|
|
|
(void)signal(signum, exit_signal_handler);
|
|
|
|
SET_ERRNO(old_errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void install_signal_handlers(void)
|
|
|
|
{
|
|
|
|
#ifdef SIGHUP
|
|
|
|
/* ignore SIGHUP signal */
|
|
|
|
if((old_sighup_handler = signal(SIGHUP, SIG_IGN)) == SIG_ERR)
|
|
|
|
logmsg("cannot install SIGHUP handler: %s", strerror(ERRNO));
|
|
|
|
#endif
|
|
|
|
#ifdef SIGPIPE
|
|
|
|
/* ignore SIGPIPE signal */
|
|
|
|
if((old_sigpipe_handler = signal(SIGPIPE, SIG_IGN)) == SIG_ERR)
|
|
|
|
logmsg("cannot install SIGPIPE handler: %s", strerror(ERRNO));
|
|
|
|
#endif
|
|
|
|
#ifdef SIGINT
|
|
|
|
/* handle SIGINT signal with our exit_signal_handler */
|
|
|
|
if((old_sigint_handler = signal(SIGINT, exit_signal_handler)) == SIG_ERR)
|
|
|
|
logmsg("cannot install SIGINT handler: %s", strerror(ERRNO));
|
|
|
|
else
|
|
|
|
siginterrupt(SIGINT, 1);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGTERM
|
|
|
|
/* handle SIGTERM signal with our exit_signal_handler */
|
|
|
|
if((old_sigterm_handler = signal(SIGTERM, exit_signal_handler)) == SIG_ERR)
|
|
|
|
logmsg("cannot install SIGTERM handler: %s", strerror(ERRNO));
|
|
|
|
else
|
|
|
|
siginterrupt(SIGTERM, 1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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-16 04:32:59 +08: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-20 02:20:38 +08:00
|
|
|
return &bfs[0].buf.hdr;
|
2005-09-16 04:32:59 +08:00
|
|
|
}
|
|
|
|
|
2009-12-02 23:02:30 +08: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-16 04:32:59 +08: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-25 05:46:42 +08:00
|
|
|
int convert /* if true, convert to ascii */)
|
2005-09-16 04:32:59 +08:00
|
|
|
{
|
|
|
|
struct bf *b;
|
|
|
|
|
|
|
|
bfs[current].counter = BF_FREE; /* free old one */
|
|
|
|
current = !current; /* "incr" current */
|
|
|
|
|
|
|
|
b = &bfs[current]; /* look at new buffer */
|
|
|
|
if (b->counter == BF_FREE) /* if it's empty */
|
2009-04-25 05:46:42 +08:00
|
|
|
read_ahead(test, convert); /* fill it */
|
2005-09-16 04:32:59 +08:00
|
|
|
|
2010-11-20 02:20:38 +08:00
|
|
|
*dpp = &b->buf.hdr; /* set caller's ptr */
|
2005-09-16 04:32:59 +08:00
|
|
|
return b->counter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* fill the input buffer, doing ascii conversions if requested
|
|
|
|
* conversions are lf -> cr,lf and cr -> cr, nul
|
|
|
|
*/
|
|
|
|
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 */
|
|
|
|
if (b->counter != BF_FREE) /* nop if not free */
|
|
|
|
return;
|
|
|
|
nextone = !nextone; /* "incr" next buffer ptr */
|
|
|
|
|
2010-11-20 02:20:38 +08:00
|
|
|
dp = &b->buf.hdr;
|
2005-09-16 04:32:59 +08:00
|
|
|
|
|
|
|
if (convert == 0) {
|
|
|
|
/* The former file reading code did this:
|
|
|
|
b->counter = read(fileno(file), dp->th_data, SEGSIZE); */
|
2005-12-05 22:10:48 +08:00
|
|
|
size_t copy_n = MIN(SEGSIZE, test->rcount);
|
2005-09-16 04:32:59 +08:00
|
|
|
memcpy(dp->th_data, test->rptr, copy_n);
|
|
|
|
|
|
|
|
/* decrease amount, advance pointer */
|
|
|
|
test->rcount -= copy_n;
|
|
|
|
test->rptr += copy_n;
|
2005-12-06 15:47:37 +08:00
|
|
|
b->counter = (int)copy_n;
|
2005-09-16 04:32:59 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = dp->th_data;
|
|
|
|
for (i = 0 ; i < SEGSIZE; i++) {
|
|
|
|
if (newline) {
|
|
|
|
if (prevchar == '\n')
|
|
|
|
c = '\n'; /* lf to cr,lf */
|
|
|
|
else
|
|
|
|
c = '\0'; /* cr to cr,nul */
|
|
|
|
newline = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(test->rcount) {
|
|
|
|
c=test->rptr[0];
|
|
|
|
test->rptr++;
|
|
|
|
test->rcount--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
if (c == '\n' || c == '\r') {
|
|
|
|
prevchar = c;
|
|
|
|
c = '\r';
|
|
|
|
newline = 1;
|
|
|
|
}
|
|
|
|
}
|
2007-02-03 01:16:06 +08:00
|
|
|
*p++ = (char)c;
|
2005-09-16 04:32:59 +08: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.
|
|
|
|
*/
|
|
|
|
static int writeit(struct testcase *test, struct tftphdr **dpp,
|
|
|
|
int ct, int convert)
|
|
|
|
{
|
|
|
|
bfs[current].counter = ct; /* set size of data to write */
|
|
|
|
current = !current; /* switch to other buffer */
|
|
|
|
if (bfs[current].counter != BF_FREE) /* if not free */
|
|
|
|
write_behind(test, convert); /* flush it */
|
|
|
|
bfs[current].counter = BF_ALLOC; /* mark as alloc'd */
|
2010-11-20 02:20:38 +08:00
|
|
|
*dpp = &bfs[current].buf.hdr;
|
2005-09-16 04:32:59 +08:00
|
|
|
return ct; /* this is a lie of course */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Output a buffer to a file, converting from netascii if requested.
|
|
|
|
* CR,NUL -> CR and CR,LF => LF.
|
|
|
|
* 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-09 00:43:32 +08:00
|
|
|
static ssize_t write_behind(struct testcase *test, int convert)
|
2005-09-16 04:32:59 +08:00
|
|
|
{
|
2007-09-27 09:45:22 +08:00
|
|
|
char *writebuf;
|
2005-09-16 04:32:59 +08:00
|
|
|
int count;
|
|
|
|
int ct;
|
|
|
|
char *p;
|
|
|
|
int c; /* current character */
|
|
|
|
struct bf *b;
|
|
|
|
struct tftphdr *dp;
|
|
|
|
|
|
|
|
b = &bfs[nextone];
|
|
|
|
if (b->counter < -1) /* anything to flush? */
|
|
|
|
return 0; /* just nop if nothing to do */
|
|
|
|
|
|
|
|
if(!test->ofile) {
|
|
|
|
char outfile[256];
|
|
|
|
snprintf(outfile, sizeof(outfile), "log/upload.%ld", test->num);
|
|
|
|
test->ofile=open(outfile, O_CREAT|O_RDWR, 0777);
|
|
|
|
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-20 02:20:38 +08:00
|
|
|
dp = &b->buf.hdr;
|
2005-09-16 04:32:59 +08:00
|
|
|
nextone = !nextone; /* incr for next time */
|
2007-09-27 09:45:22 +08:00
|
|
|
writebuf = dp->th_data;
|
2005-09-16 04:32:59 +08:00
|
|
|
|
|
|
|
if (count <= 0)
|
|
|
|
return -1; /* nak logic? */
|
|
|
|
|
|
|
|
if (convert == 0)
|
2007-09-27 09:45:22 +08:00
|
|
|
return write(test->ofile, writebuf, count);
|
2005-09-16 04:32:59 +08:00
|
|
|
|
2007-09-27 09:45:22 +08:00
|
|
|
p = writebuf;
|
2005-09-16 04:32:59 +08:00
|
|
|
ct = count;
|
|
|
|
while (ct--) { /* loop over the buffer */
|
|
|
|
c = *p++; /* pick up a character */
|
|
|
|
if (prevchar == '\r') { /* if prev char was cr */
|
|
|
|
if (c == '\n') /* if have cr,lf then just */
|
|
|
|
lseek(test->ofile, -1, SEEK_CUR); /* smash lf on top of the cr */
|
|
|
|
else
|
|
|
|
if (c == '\0') /* if have cr,nul then */
|
|
|
|
goto skipit; /* just skip over the putc */
|
|
|
|
/* else just fall through and allow it */
|
|
|
|
}
|
|
|
|
/* formerly
|
|
|
|
putc(c, file); */
|
2011-06-12 05:01:09 +08:00
|
|
|
if(1 != write(test->ofile, &c, 1))
|
|
|
|
break;
|
2005-09-16 04:32:59 +08: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-04 06:19:15 +08:00
|
|
|
|
|
|
|
#if defined(HAVE_IOCTLSOCKET)
|
|
|
|
unsigned long i;
|
|
|
|
#else
|
|
|
|
int i;
|
|
|
|
#endif
|
|
|
|
int j = 0;
|
2005-09-16 04:32:59 +08:00
|
|
|
char rbuf[PKTSIZE];
|
2010-11-20 02:20:38 +08:00
|
|
|
srvr_sockaddr_union_t fromaddr;
|
2009-05-02 10:37:32 +08:00
|
|
|
curl_socklen_t fromaddrlen;
|
2005-09-16 04:32:59 +08:00
|
|
|
|
2009-11-24 00:11:50 +08:00
|
|
|
for (;;) {
|
2006-01-04 06:19:15 +08:00
|
|
|
#if defined(HAVE_IOCTLSOCKET)
|
|
|
|
(void) ioctlsocket(f, FIONREAD, &i);
|
2006-01-04 06:44:58 +08:00
|
|
|
#else
|
2005-09-16 04:32:59 +08:00
|
|
|
(void) ioctl(f, FIONREAD, &i);
|
2006-01-04 06:19:15 +08:00
|
|
|
#endif
|
2005-09-16 04:32:59 +08:00
|
|
|
if (i) {
|
|
|
|
j++;
|
2010-11-20 02:20:38 +08: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-16 04:32:59 +08:00
|
|
|
}
|
|
|
|
else
|
2005-12-20 03:47:14 +08:00
|
|
|
break;
|
2005-09-16 04:32:59 +08:00
|
|
|
}
|
2005-12-20 03:47:14 +08:00
|
|
|
return j;
|
2005-09-16 04:32:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2010-11-20 02:20:38 +08:00
|
|
|
srvr_sockaddr_union_t me;
|
2005-09-16 04:32:59 +08:00
|
|
|
struct tftphdr *tp;
|
2008-07-21 11:06:07 +08:00
|
|
|
ssize_t n = 0;
|
2005-09-16 04:32:59 +08:00
|
|
|
int arg = 1;
|
|
|
|
unsigned short port = DEFAULT_PORT;
|
2009-11-24 00:11:50 +08:00
|
|
|
curl_socket_t sock = CURL_SOCKET_BAD;
|
2005-09-16 04:32:59 +08:00
|
|
|
int flag;
|
|
|
|
int rc;
|
2009-11-24 00:11:50 +08:00
|
|
|
int error;
|
2009-12-02 23:02:30 +08:00
|
|
|
long pid;
|
2005-09-16 04:32:59 +08:00
|
|
|
struct testcase test;
|
2008-04-24 07:55:34 +08:00
|
|
|
int result = 0;
|
2005-09-16 04:32:59 +08:00
|
|
|
|
2009-12-04 04:37:49 +08:00
|
|
|
memset(&test, 0, sizeof(test));
|
|
|
|
|
2005-09-16 04:32:59 +08: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-08 09:48:54 +08: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-16 04:32:59 +08:00
|
|
|
else if(!strcmp("--ipv6", argv[arg])) {
|
|
|
|
#ifdef ENABLE_IPV6
|
2008-09-26 19:21:22 +08:00
|
|
|
ipv_inuse = "IPv6";
|
|
|
|
use_ipv6 = TRUE;
|
2005-09-16 04:32:59 +08:00
|
|
|
#endif
|
|
|
|
arg++;
|
|
|
|
}
|
2010-01-08 09:48:54 +08:00
|
|
|
else if(!strcmp("--port", argv[arg])) {
|
|
|
|
arg++;
|
|
|
|
if(argc>arg) {
|
2010-02-05 01:17:19 +08:00
|
|
|
char *endptr;
|
2010-02-18 20:31:24 +08:00
|
|
|
unsigned long ulnum = strtoul(argv[arg], &endptr, 10);
|
2010-02-05 01:17:19 +08:00
|
|
|
if((endptr != argv[arg] + strlen(argv[arg])) ||
|
2010-02-18 20:31:24 +08:00
|
|
|
(ulnum < 1025UL) || (ulnum > 65535UL)) {
|
2010-02-05 01:17:19 +08:00
|
|
|
fprintf(stderr, "tftpd: invalid --port argument (%s)\n",
|
|
|
|
argv[arg]);
|
|
|
|
return 0;
|
|
|
|
}
|
2010-02-23 02:56:29 +08:00
|
|
|
port = curlx_ultous(ulnum);
|
2010-01-08 09:48:54 +08: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 20:01:35 +08:00
|
|
|
" --srcdir [path]");
|
2010-01-08 09:48:54 +08:00
|
|
|
return 0;
|
2005-09-16 04:32:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-12 00:01:16 +08:00
|
|
|
#ifdef WIN32
|
2005-09-16 04:32:59 +08:00
|
|
|
win32_init();
|
|
|
|
atexit(win32_cleanup);
|
|
|
|
#endif
|
|
|
|
|
2009-12-02 23:02:30 +08:00
|
|
|
install_signal_handlers();
|
|
|
|
|
|
|
|
pid = (long)getpid();
|
|
|
|
|
2005-09-16 04:32:59 +08: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-12 01:46:33 +08:00
|
|
|
if(CURL_SOCKET_BAD == sock) {
|
2009-11-24 00:11:50 +08:00
|
|
|
error = SOCKERRNO;
|
|
|
|
logmsg("Error creating socket: (%d) %s",
|
|
|
|
error, strerror(error));
|
2009-12-02 23:02:30 +08:00
|
|
|
result = 1;
|
|
|
|
goto tftpd_cleanup;
|
2005-09-16 04:32:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
flag = 1;
|
2009-11-24 00:11:50 +08:00
|
|
|
if (0 != setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
|
|
|
|
(void *)&flag, sizeof(flag))) {
|
|
|
|
error = SOCKERRNO;
|
|
|
|
logmsg("setsockopt(SO_REUSEADDR) failed with error: (%d) %s",
|
|
|
|
error, strerror(error));
|
2009-12-02 23:02:30 +08:00
|
|
|
result = 1;
|
|
|
|
goto tftpd_cleanup;
|
2005-09-16 04:32:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
if(!use_ipv6) {
|
|
|
|
#endif
|
2010-11-20 02:20:38 +08: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-16 04:32:59 +08:00
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
}
|
|
|
|
else {
|
2010-11-20 02:20:38 +08: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-16 04:32:59 +08:00
|
|
|
}
|
|
|
|
#endif /* ENABLE_IPV6 */
|
2009-11-24 00:11:50 +08:00
|
|
|
if(0 != rc) {
|
|
|
|
error = SOCKERRNO;
|
|
|
|
logmsg("Error binding socket on port %hu: (%d) %s",
|
|
|
|
port, error, strerror(error));
|
2009-12-02 23:02:30 +08:00
|
|
|
result = 1;
|
|
|
|
goto tftpd_cleanup;
|
2005-09-16 04:32:59 +08:00
|
|
|
}
|
|
|
|
|
2009-12-02 23:02:30 +08:00
|
|
|
wrotepidfile = write_pidfile(pidname);
|
|
|
|
if(!wrotepidfile) {
|
|
|
|
result = 1;
|
|
|
|
goto tftpd_cleanup;
|
2007-02-19 10:03:58 +08:00
|
|
|
}
|
2005-09-16 04:32:59 +08:00
|
|
|
|
2008-09-26 19:21:22 +08:00
|
|
|
logmsg("Running %s version on port UDP/%d", ipv_inuse, (int)port);
|
2005-09-16 04:32:59 +08:00
|
|
|
|
2009-11-24 00:11:50 +08:00
|
|
|
for (;;) {
|
2008-07-30 13:10:42 +08:00
|
|
|
fromlen = sizeof(from);
|
2010-11-20 02:20:38 +08: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 23:02:30 +08:00
|
|
|
if(got_exit_signal)
|
|
|
|
break;
|
2005-09-16 04:32:59 +08:00
|
|
|
if (n < 0) {
|
2009-04-25 05:46:42 +08:00
|
|
|
logmsg("recvfrom");
|
2008-04-24 07:55:34 +08:00
|
|
|
result = 3;
|
|
|
|
break;
|
2005-09-16 04:32:59 +08:00
|
|
|
}
|
|
|
|
|
2008-04-24 07:55:34 +08:00
|
|
|
set_advisor_read_lock(SERVERLOGS_LOCK);
|
2009-11-26 18:15:08 +08:00
|
|
|
serverlogslocked = 1;
|
2008-04-24 07:55:34 +08:00
|
|
|
|
2010-11-20 02:20:38 +08: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-16 04:32:59 +08:00
|
|
|
}
|
2010-11-20 02:20:38 +08: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;
|
|
|
|
}
|
|
|
|
if (connect(peer, &from.sa, sizeof(from.sa6)) < 0) {
|
|
|
|
logmsg("connect: fail");
|
|
|
|
result = 1;
|
|
|
|
break;
|
|
|
|
}
|
2005-09-16 04:32:59 +08:00
|
|
|
}
|
2010-11-20 02:20:38 +08:00
|
|
|
#endif
|
|
|
|
|
2005-09-20 06:03:16 +08:00
|
|
|
maxtimeout = 5*TIMEOUT;
|
2005-09-16 04:32:59 +08:00
|
|
|
|
2010-11-20 02:20:38 +08:00
|
|
|
tp = &buf.hdr;
|
2005-09-16 04:32:59 +08:00
|
|
|
tp->th_opcode = ntohs(tp->th_opcode);
|
2008-10-23 22:07:28 +08:00
|
|
|
if (tp->th_opcode == opcode_RRQ || tp->th_opcode == opcode_WRQ) {
|
2005-09-16 04:32:59 +08:00
|
|
|
memset(&test, 0, sizeof(test));
|
2009-12-02 23:02:30 +08:00
|
|
|
if (do_tftp(&test, tp, n) < 0)
|
2005-09-20 06:03:16 +08:00
|
|
|
break;
|
|
|
|
if(test.buffer)
|
|
|
|
free(test.buffer);
|
2005-09-16 04:32:59 +08:00
|
|
|
}
|
|
|
|
sclose(peer);
|
2009-12-02 23:02:30 +08:00
|
|
|
peer = CURL_SOCKET_BAD;
|
|
|
|
|
2009-12-04 04:37:49 +08:00
|
|
|
if(test.ofile > 0) {
|
|
|
|
close(test.ofile);
|
|
|
|
test.ofile = 0;
|
|
|
|
}
|
|
|
|
|
2009-12-02 23:02:30 +08:00
|
|
|
if(got_exit_signal)
|
|
|
|
break;
|
2008-04-24 07:55:34 +08:00
|
|
|
|
2009-11-26 18:15:08 +08:00
|
|
|
if(serverlogslocked) {
|
|
|
|
serverlogslocked = 0;
|
|
|
|
clear_advisor_read_lock(SERVERLOGS_LOCK);
|
|
|
|
}
|
2008-04-24 07:55:34 +08:00
|
|
|
|
2009-04-25 05:46:42 +08:00
|
|
|
logmsg("end of one transfer");
|
|
|
|
|
2009-11-24 00:11:50 +08:00
|
|
|
}
|
2008-04-24 07:55:34 +08:00
|
|
|
|
2009-12-02 23:02:30 +08:00
|
|
|
tftpd_cleanup:
|
|
|
|
|
2009-12-04 04:37:49 +08:00
|
|
|
if(test.ofile > 0)
|
|
|
|
close(test.ofile);
|
|
|
|
|
2009-12-02 23:02:30 +08: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 18:15:08 +08:00
|
|
|
if(serverlogslocked) {
|
|
|
|
serverlogslocked = 0;
|
|
|
|
clear_advisor_read_lock(SERVERLOGS_LOCK);
|
|
|
|
}
|
2008-04-24 07:55:34 +08:00
|
|
|
|
2009-12-02 23:02:30 +08: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-24 07:55:34 +08:00
|
|
|
return result;
|
2005-09-16 04:32:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle initial connection protocol.
|
|
|
|
*/
|
2009-12-02 23:02:30 +08:00
|
|
|
static int do_tftp(struct testcase *test, struct tftphdr *tp, ssize_t size)
|
2005-09-16 04:32:59 +08:00
|
|
|
{
|
|
|
|
char *cp;
|
|
|
|
int first = 1, ecode;
|
|
|
|
struct formats *pf;
|
|
|
|
char *filename, *mode = NULL;
|
2007-10-24 21:03:08 +08:00
|
|
|
int error;
|
|
|
|
FILE *server;
|
|
|
|
|
|
|
|
/* Open request dump file. */
|
|
|
|
server = fopen(REQUEST_DUMP, "ab");
|
|
|
|
if(!server) {
|
|
|
|
error = ERRNO;
|
|
|
|
logmsg("fopen() failed with error: %d %s", error, strerror(error));
|
|
|
|
logmsg("Error opening file: %s", REQUEST_DUMP);
|
|
|
|
return -1;
|
|
|
|
}
|
2005-09-16 04:32:59 +08:00
|
|
|
|
|
|
|
/* store input protocol */
|
2007-10-24 21:03:08 +08:00
|
|
|
fprintf(server, "opcode: %x\n", tp->th_opcode);
|
2005-09-16 04:32:59 +08:00
|
|
|
|
2005-09-16 18:50:43 +08:00
|
|
|
cp = (char *)&tp->th_stuff;
|
|
|
|
filename = cp;
|
2005-09-16 04:32:59 +08:00
|
|
|
again:
|
2010-11-20 02:20:38 +08:00
|
|
|
while (cp < &buf.storage[size]) {
|
2005-09-16 04:32:59 +08:00
|
|
|
if (*cp == '\0')
|
|
|
|
break;
|
|
|
|
cp++;
|
|
|
|
}
|
|
|
|
if (*cp) {
|
|
|
|
nak(EBADOP);
|
2007-10-24 21:03:08 +08:00
|
|
|
fclose(server);
|
2005-09-16 04:32:59 +08:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
if (first) {
|
|
|
|
mode = ++cp;
|
|
|
|
first = 0;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
/* store input protocol */
|
2007-10-24 21:03:08 +08:00
|
|
|
fprintf(server, "filename: %s\n", filename);
|
2005-09-16 04:32:59 +08:00
|
|
|
|
2009-09-17 23:33:32 +08:00
|
|
|
for (cp = mode; cp && *cp; cp++)
|
2007-11-15 21:20:18 +08:00
|
|
|
if(ISUPPER(*cp))
|
2007-02-03 01:16:06 +08:00
|
|
|
*cp = (char)tolower((int)*cp);
|
2005-09-16 04:32:59 +08:00
|
|
|
|
|
|
|
/* store input protocol */
|
2007-10-24 21:03:08 +08:00
|
|
|
fprintf(server, "mode: %s\n", mode);
|
|
|
|
fclose(server);
|
2005-09-16 04:32:59 +08:00
|
|
|
|
2008-09-20 20:33:02 +08:00
|
|
|
for (pf = formata; pf->f_mode; pf++)
|
2005-09-16 04:32:59 +08:00
|
|
|
if (strcmp(pf->f_mode, mode) == 0)
|
|
|
|
break;
|
|
|
|
if (!pf->f_mode) {
|
|
|
|
nak(EBADOP);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
ecode = validate_access(test, filename, tp->th_opcode);
|
|
|
|
if (ecode) {
|
|
|
|
nak(ecode);
|
|
|
|
return 1;
|
|
|
|
}
|
2008-10-23 22:07:28 +08:00
|
|
|
if (tp->th_opcode == opcode_WRQ)
|
2005-09-16 18:52:38 +08:00
|
|
|
recvtftp(test, pf);
|
2005-09-16 04:32:59 +08:00
|
|
|
else
|
2005-09-16 18:52:38 +08:00
|
|
|
sendtftp(test, pf);
|
2005-09-20 06:03:16 +08:00
|
|
|
|
2005-09-16 04:32:59 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Validate file access.
|
|
|
|
*/
|
|
|
|
static int validate_access(struct testcase *test,
|
|
|
|
const char *filename, int mode)
|
|
|
|
{
|
|
|
|
char *ptr;
|
2007-10-25 06:48:23 +08:00
|
|
|
long testno, partno;
|
2007-02-19 10:03:58 +08:00
|
|
|
int error;
|
2007-10-25 06:48:23 +08:00
|
|
|
char partbuf[80]="data";
|
2005-09-16 04:32:59 +08:00
|
|
|
|
|
|
|
logmsg("trying to get file: %s mode %x", filename, mode);
|
|
|
|
|
2008-02-07 01:35:17 +08:00
|
|
|
if(!strncmp("verifiedserver", filename, 14)) {
|
2005-09-16 04:32:59 +08:00
|
|
|
char weare[128];
|
2008-02-07 00:54:01 +08:00
|
|
|
size_t count = sprintf(weare, "WE ROOLZ: %ld\r\n", (long)getpid());
|
2005-09-16 04:32:59 +08: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-24 03:14:54 +08:00
|
|
|
while(*ptr && !ISDIGIT(*ptr))
|
2005-09-16 04:32:59 +08:00
|
|
|
ptr++;
|
|
|
|
|
|
|
|
/* get the number */
|
|
|
|
testno = strtol(ptr, &ptr, 10);
|
|
|
|
|
2007-10-25 06:48:23 +08:00
|
|
|
if(testno > 10000) {
|
|
|
|
partno = testno % 10000;
|
|
|
|
testno /= 10000;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
partno = 0;
|
|
|
|
|
|
|
|
|
|
|
|
logmsg("requested test number %ld part %ld", testno, partno);
|
2005-09-16 04:32:59 +08:00
|
|
|
|
|
|
|
test->num = testno;
|
|
|
|
|
|
|
|
file = test2file(testno);
|
|
|
|
|
2007-10-25 06:48:23 +08:00
|
|
|
if(0 != partno)
|
|
|
|
sprintf(partbuf, "data%ld", partno);
|
|
|
|
|
2005-09-16 04:32:59 +08:00
|
|
|
if(file) {
|
|
|
|
FILE *stream=fopen(file, "rb");
|
|
|
|
if(!stream) {
|
2007-02-19 10:03:58 +08:00
|
|
|
error = ERRNO;
|
|
|
|
logmsg("fopen() failed with error: %d %s", error, strerror(error));
|
|
|
|
logmsg("Error opening file: %s", file);
|
2005-09-16 04:32:59 +08:00
|
|
|
logmsg("Couldn't open test file: %s", file);
|
|
|
|
return EACCESS;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
size_t count;
|
2010-02-14 21:14:17 +08:00
|
|
|
error = getpart(&test->buffer, &count, "reply", partbuf, stream);
|
2005-09-16 04:32:59 +08:00
|
|
|
fclose(stream);
|
2010-02-14 21:14:17 +08:00
|
|
|
if(error) {
|
|
|
|
logmsg("getpart() failed with error: %d", error);
|
|
|
|
return EACCESS;
|
|
|
|
}
|
2005-09-16 04:32:59 +08: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-25 05:46:42 +08:00
|
|
|
logmsg("file opened and all is good");
|
2005-09-16 04:32:59 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Send the requested file.
|
|
|
|
*/
|
2005-09-16 18:52:38 +08:00
|
|
|
static void sendtftp(struct testcase *test, struct formats *pf)
|
2005-09-16 04:32:59 +08:00
|
|
|
{
|
2006-07-29 02:01:23 +08:00
|
|
|
int size;
|
|
|
|
ssize_t n;
|
2008-10-15 01:35:25 +08:00
|
|
|
sendblock = 1;
|
2006-01-04 06:19:15 +08:00
|
|
|
#if defined(HAVE_ALARM) && defined(SIGALRM)
|
2005-09-16 04:32:59 +08:00
|
|
|
mysignal(SIGALRM, timer);
|
2006-01-04 06:19:15 +08:00
|
|
|
#endif
|
2008-10-15 01:35:25 +08:00
|
|
|
sdp = r_init();
|
2010-11-20 02:20:38 +08:00
|
|
|
sap = &ackbuf.hdr;
|
2005-09-16 04:32:59 +08:00
|
|
|
do {
|
2008-10-15 01:35:25 +08:00
|
|
|
size = readit(test, &sdp, pf->f_convert);
|
2005-09-16 04:32:59 +08:00
|
|
|
if (size < 0) {
|
2007-02-17 00:01:19 +08:00
|
|
|
nak(ERRNO + 100);
|
2005-09-20 06:03:16 +08:00
|
|
|
return;
|
2005-09-16 04:32:59 +08:00
|
|
|
}
|
2011-04-06 13:30:09 +08:00
|
|
|
sdp->th_opcode = htons((unsigned short)opcode_DATA);
|
|
|
|
sdp->th_block = htons(sendblock);
|
2005-09-16 04:32:59 +08:00
|
|
|
timeout = 0;
|
2006-01-04 06:44:58 +08:00
|
|
|
#ifdef HAVE_SIGSETJMP
|
2005-09-16 04:32:59 +08:00
|
|
|
(void) sigsetjmp(timeoutbuf, 1);
|
2006-01-04 06:19:15 +08:00
|
|
|
#endif
|
2005-09-16 04:32:59 +08:00
|
|
|
send_data:
|
2008-10-15 01:35:25 +08:00
|
|
|
if (swrite(peer, sdp, size + 4) != size + 4) {
|
2009-04-25 05:46:42 +08:00
|
|
|
logmsg("write");
|
2005-09-20 06:03:16 +08:00
|
|
|
return;
|
2005-09-16 04:32:59 +08:00
|
|
|
}
|
|
|
|
read_ahead(test, pf->f_convert);
|
|
|
|
for ( ; ; ) {
|
2006-01-04 06:44:58 +08:00
|
|
|
#ifdef HAVE_ALARM
|
2005-09-16 04:32:59 +08:00
|
|
|
alarm(rexmtval); /* read the ack */
|
2006-01-04 06:19:15 +08:00
|
|
|
#endif
|
2010-11-20 02:20:38 +08:00
|
|
|
n = sread(peer, &ackbuf.storage[0], sizeof(ackbuf.storage));
|
2006-01-04 06:44:58 +08:00
|
|
|
#ifdef HAVE_ALARM
|
2005-09-16 04:32:59 +08:00
|
|
|
alarm(0);
|
2006-01-04 06:44:58 +08:00
|
|
|
#endif
|
2009-12-02 23:02:30 +08:00
|
|
|
if(got_exit_signal)
|
|
|
|
return;
|
2005-09-16 04:32:59 +08:00
|
|
|
if (n < 0) {
|
2009-04-25 05:46:42 +08:00
|
|
|
logmsg("read: fail");
|
2005-09-20 06:03:16 +08:00
|
|
|
return;
|
2005-09-16 04:32:59 +08:00
|
|
|
}
|
2011-04-06 13:30:09 +08:00
|
|
|
sap->th_opcode = ntohs((unsigned short)sap->th_opcode);
|
|
|
|
sap->th_block = ntohs(sap->th_block);
|
2005-09-16 04:32:59 +08:00
|
|
|
|
2008-10-23 22:07:28 +08:00
|
|
|
if (sap->th_opcode == opcode_ERROR) {
|
2005-09-20 06:03:16 +08:00
|
|
|
logmsg("got ERROR");
|
|
|
|
return;
|
|
|
|
}
|
2005-09-16 04:32:59 +08:00
|
|
|
|
2008-10-23 22:07:28 +08:00
|
|
|
if (sap->th_opcode == opcode_ACK) {
|
2008-10-15 01:35:25 +08:00
|
|
|
if (sap->th_block == sendblock) {
|
2005-09-16 04:32:59 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Re-synchronize with the other side */
|
|
|
|
(void) synchnet(peer);
|
2008-10-15 01:35:25 +08:00
|
|
|
if (sap->th_block == (sendblock-1)) {
|
2005-09-16 04:32:59 +08:00
|
|
|
goto send_data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2008-10-15 01:35:25 +08:00
|
|
|
sendblock++;
|
2005-09-16 04:32:59 +08:00
|
|
|
} while (size == SEGSIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Receive a file.
|
|
|
|
*/
|
2005-09-16 18:52:38 +08:00
|
|
|
static void recvtftp(struct testcase *test, struct formats *pf)
|
2005-09-16 04:32:59 +08:00
|
|
|
{
|
2006-07-29 17:15:03 +08:00
|
|
|
ssize_t n, size;
|
2008-10-15 01:35:25 +08:00
|
|
|
recvblock = 0;
|
2006-01-04 06:19:15 +08:00
|
|
|
#if defined(HAVE_ALARM) && defined(SIGALRM)
|
2005-09-16 04:32:59 +08:00
|
|
|
mysignal(SIGALRM, timer);
|
2006-01-04 06:19:15 +08:00
|
|
|
#endif
|
2008-10-15 01:35:25 +08:00
|
|
|
rdp = w_init();
|
2010-11-20 02:20:38 +08:00
|
|
|
rap = &ackbuf.hdr;
|
2005-09-16 04:32:59 +08:00
|
|
|
do {
|
|
|
|
timeout = 0;
|
2011-04-06 13:30:09 +08:00
|
|
|
rap->th_opcode = htons((unsigned short)opcode_ACK);
|
|
|
|
rap->th_block = htons(recvblock);
|
2008-10-15 01:35:25 +08:00
|
|
|
recvblock++;
|
2006-01-04 06:19:15 +08:00
|
|
|
#ifdef HAVE_SIGSETJMP
|
2005-09-16 04:32:59 +08:00
|
|
|
(void) sigsetjmp(timeoutbuf, 1);
|
2006-01-04 06:19:15 +08:00
|
|
|
#endif
|
2005-09-16 04:32:59 +08:00
|
|
|
send_ack:
|
2010-11-20 02:20:38 +08:00
|
|
|
if (swrite(peer, &ackbuf.storage[0], 4) != 4) {
|
2005-09-16 04:32:59 +08:00
|
|
|
logmsg("write: fail\n");
|
|
|
|
goto abort;
|
|
|
|
}
|
|
|
|
write_behind(test, pf->f_convert);
|
|
|
|
for ( ; ; ) {
|
2006-01-04 06:44:58 +08:00
|
|
|
#ifdef HAVE_ALARM
|
2005-09-16 04:32:59 +08:00
|
|
|
alarm(rexmtval);
|
2006-01-04 06:44:58 +08:00
|
|
|
#endif
|
2008-10-15 01:35:25 +08:00
|
|
|
n = sread(peer, rdp, PKTSIZE);
|
2006-01-04 06:44:58 +08:00
|
|
|
#ifdef HAVE_ALARM
|
2005-09-16 04:32:59 +08:00
|
|
|
alarm(0);
|
2006-01-04 06:19:15 +08:00
|
|
|
#endif
|
2009-12-02 23:02:30 +08:00
|
|
|
if(got_exit_signal)
|
|
|
|
goto abort;
|
2005-09-16 04:32:59 +08:00
|
|
|
if (n < 0) { /* really? */
|
|
|
|
logmsg("read: fail\n");
|
|
|
|
goto abort;
|
|
|
|
}
|
2011-04-06 13:30:09 +08:00
|
|
|
rdp->th_opcode = ntohs((unsigned short)rdp->th_opcode);
|
|
|
|
rdp->th_block = ntohs(rdp->th_block);
|
2008-10-23 22:07:28 +08:00
|
|
|
if (rdp->th_opcode == opcode_ERROR)
|
2005-09-16 04:32:59 +08:00
|
|
|
goto abort;
|
2008-10-23 22:07:28 +08:00
|
|
|
if (rdp->th_opcode == opcode_DATA) {
|
2008-10-15 01:35:25 +08:00
|
|
|
if (rdp->th_block == recvblock) {
|
2005-09-16 04:32:59 +08:00
|
|
|
break; /* normal */
|
|
|
|
}
|
|
|
|
/* Re-synchronize with the other side */
|
|
|
|
(void) synchnet(peer);
|
2008-10-15 01:35:25 +08:00
|
|
|
if (rdp->th_block == (recvblock-1))
|
2005-09-16 04:32:59 +08:00
|
|
|
goto send_ack; /* rexmit */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-15 01:35:25 +08:00
|
|
|
size = writeit(test, &rdp, (int)(n - 4), pf->f_convert);
|
2005-09-16 04:32:59 +08:00
|
|
|
if (size != (n-4)) { /* ahem */
|
|
|
|
if (size < 0)
|
2007-02-17 00:01:19 +08:00
|
|
|
nak(ERRNO + 100);
|
2005-09-16 04:32:59 +08:00
|
|
|
else
|
|
|
|
nak(ENOSPACE);
|
|
|
|
goto abort;
|
|
|
|
}
|
|
|
|
} while (size == SEGSIZE);
|
|
|
|
write_behind(test, pf->f_convert);
|
|
|
|
|
2011-04-06 13:30:09 +08:00
|
|
|
rap->th_opcode = htons((unsigned short)opcode_ACK); /* send the "final" ack */
|
|
|
|
rap->th_block = htons(recvblock);
|
2010-11-20 02:20:38 +08:00
|
|
|
(void) swrite(peer, &ackbuf.storage[0], 4);
|
2006-01-04 06:19:15 +08:00
|
|
|
#if defined(HAVE_ALARM) && defined(SIGALRM)
|
2007-01-16 05:03:53 +08:00
|
|
|
mysignal(SIGALRM, justtimeout); /* just abort read on timeout */
|
2005-09-16 04:32:59 +08:00
|
|
|
alarm(rexmtval);
|
2006-01-04 06:44:58 +08:00
|
|
|
#endif
|
2010-11-20 02:20:38 +08:00
|
|
|
/* normally times out and quits */
|
|
|
|
n = sread(peer, &buf.storage[0], sizeof(buf.storage));
|
2006-01-04 06:44:58 +08:00
|
|
|
#ifdef HAVE_ALARM
|
2005-09-16 04:32:59 +08:00
|
|
|
alarm(0);
|
2006-01-04 06:44:58 +08:00
|
|
|
#endif
|
2009-12-02 23:02:30 +08:00
|
|
|
if(got_exit_signal)
|
|
|
|
goto abort;
|
2010-11-20 02:20:38 +08: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 */
|
|
|
|
(void) swrite(peer, &ackbuf.storage[0], 4); /* resend final ack */
|
2005-09-16 04:32:59 +08:00
|
|
|
}
|
|
|
|
abort:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Send a nak packet (error message). Error code passed in is one of the
|
|
|
|
* standard TFTP codes, or a UNIX errno offset by 100.
|
|
|
|
*/
|
|
|
|
static void nak(int error)
|
|
|
|
{
|
|
|
|
struct tftphdr *tp;
|
|
|
|
int length;
|
|
|
|
struct errmsg *pe;
|
|
|
|
|
2010-11-20 02:20:38 +08:00
|
|
|
tp = &buf.hdr;
|
2011-04-06 13:30:09 +08:00
|
|
|
tp->th_opcode = htons((unsigned short)opcode_ERROR);
|
|
|
|
tp->th_code = htons((unsigned short)error);
|
2005-09-16 04:32:59 +08:00
|
|
|
for (pe = errmsgs; pe->e_code >= 0; pe++)
|
|
|
|
if (pe->e_code == error)
|
|
|
|
break;
|
|
|
|
if (pe->e_code < 0) {
|
|
|
|
pe->e_msg = strerror(error - 100);
|
|
|
|
tp->th_code = EUNDEF; /* set 'undef' errorcode */
|
|
|
|
}
|
2005-12-05 22:10:48 +08:00
|
|
|
length = (int)strlen(pe->e_msg);
|
2010-12-18 02:07:45 +08: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-16 04:32:59 +08:00
|
|
|
length += 5;
|
2010-11-20 02:20:38 +08:00
|
|
|
if (swrite(peer, &buf.storage[0], length) != length)
|
2005-09-16 04:32:59 +08:00
|
|
|
logmsg("nak: fail\n");
|
|
|
|
}
|