proxy_tftpd.c revision f0c792e78a8bf77c28c7814441e514bea7c5362b
/* -*- indent-tabs-mode: nil; -*- */
#define _USE_WINSTD_ERRNO
/* XXX: replace POSIX file operations with IPRT, to avoid hacks with errno renamings */
#include "winutils.h"
#include "proxy.h"
#include "tftp.h"
#ifndef RT_OS_WINDOWS
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#else
#include <errno.h>
#include <fcntl.h>
#include <io.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#endif
struct xfer {
int fd;
unsigned int ack;
int rexmit;
char *filename;
int octet;
/* options */
unsigned int blksize;
int blksize_from_opt;
unsigned int timeout;
int timeout_from_opt;
int tsize_from_opt;
};
struct tftpd {
char *root;
#define TFTP_MAX_XFERS 3
};
struct tftp_option {
const char *name;
};
static void tftp_fillbuf(struct xfer *);
static void tftp_timeout(void *);
static int tftp_xfer_create_pcb(struct xfer *);
static void tftp_xfer_free(struct xfer *);
static int tftp_opt_blksize(struct xfer *, const char *);
static int tftp_opt_timeout(struct xfer *, const char *);
static int tftp_opt_tsize(struct xfer *, const char *);
static int tftp_add_oack(char **, size_t *, const char *, const char *, ...) __attribute__((format(printf, 4, 5)));
static int tftp_internal_error(struct xfer *);
static int tftp_error(struct xfer *, u16_t, const char *, ...) __attribute__((format(printf, 3, 4)));
static void tftpd_error(ip_addr_t *, u16_t, u16_t, const char *, ...) __attribute__((format(printf, 4, 5)));
static struct tftp_option tftp_options[] = {
};
{
return ERR_MEM;
}
}
return ERR_MEM;
}
return error;
}
return ERR_OK;
}
static void
{
pbuf_free(p);
return;
}
switch (op) {
case TFTP_RRQ:
break;
case TFTP_WRQ:
break;
default:
break;
}
pbuf_free(p);
}
/**
* Parse Read Request packet and start new transfer.
*/
static void
{
char *s;
int has_options;
int status;
return;
}
/* skip opcode */
/*
* Parse RRQ:
* filename, mode, [opt1, value1, [...] ]
*/
if (status < 0) {
goto terminate;
}
if (status < 0) {
goto terminate;
}
has_options = 0;
while (len > 0) {
if (status < 0) {
goto terminate;
}
has_options += status;
}
/*
* Create OACK packet if necessary.
*/
if (has_options) {
struct tftp_option *o;
if (status < 0) {
break;
}
}
}
}
}
/*
* Create static pbuf that will be used for all data packets.
*/
goto terminate;
}
/*
* Finally, create PCB. Before this point any error was reported
* from the server port (see tftp_error() for the reason).
*/
if (status < 0) {
goto terminate;
}
}
else {
/* trigger send of the first data packet */
tftp_recv_ack(xfer, 0);
}
return;
}
static void
{
if (p->len < 2) {
pbuf_free(p);
return;
}
if (p->len < 4) {
pbuf_free(p);
return;
}
}
else if (op == TFTP_ERROR) {
}
else {
}
pbuf_free(p);
}
static void
{
DPRINTF2(("%s: expect %u (%u), got %u\n",
return;
}
DPRINTF(("%s: got final ack %u (%u)\n",
return;
}
}
}
static void
{
}
static void
tftp_timeout(void *arg)
{
int maxrexmit;
}
else {
}
}
static void
{
if (nread < 0) {
return;
}
}
/**
* Find a free transfer slot (without a pcb). Record peer's IP
* address and port, but don't allocate a pcb yet.
*
* We delay creation of the pcb in response to the original request
* until the request is verified and accepted. This makes using
* tcpdump(8) easier, since tcpdump does not track TFTP transfers, so
* an error reply from a new pcb is not recognized as such and is not
* decoded as TFTP (see tftp_error()).
*
* If the request is rejected, the pcb remains NULL and the transfer
* slot remains unallocated. Since all TFTP processing happens on the
* lwIP thread, there's no concurrent processing, so we don't need to
* "lock" the transfer slot until the pcb is allocated.
*/
static struct xfer *
{
int i;
/* Find free xfer slot */
for (i = 0; i < TFTP_MAX_XFERS; ++i) {
break;
}
}
if (report_transient_errors) {
"Maximum number of simultaneous connections exceeded");
}
return NULL;
}
xfer->blksize_from_opt = 0;
xfer->timeout_from_opt = 0;
xfer->tsize_from_opt = 0;
return xfer;
}
static int
{
/* Bind */
}
}
/* Connect */
}
}
if (report_transient_errors) {
}
return -1;
}
return 0;
}
static void
{
}
}
}
}
}
}
static int
{
const char *filename;
char *pathname;
char *s;
int status;
return -1;
}
return tftp_internal_error(xfer);
}
/* replace backslashes with forward slashes */
*s++ = '/';
}
/* deny attempts to break out of tftp dir */
{
}
return tftp_internal_error(xfer);
}
if (status < 0) {
return tftp_internal_error(xfer);
}
}
else {
}
}
if (status < 0) {
return tftp_internal_error(xfer);
}
}
return 0;
}
static int
{
const char *modename;
return -1;
}
}
/* XXX: not (yet?) */
}
}
else {
}
return 0;
}
static int
{
const char *opt;
const char *val;
struct tftp_option *o;
return -1;
}
if (*plen == 0) {
}
return -1;
}
/* handle option if known, ignore otherwise */
}
}
return 0; /* unknown option */
}
static int
{
char *end;
long blksize;
errno = 0;
return 0;
}
if (blksize < 8) {
return 0;
}
blksize = 1428;
}
return 1;
}
static int
{
return 0;
}
static int
{
return 0;
}
return 1;
}
static char *
{
char *s;
s = *ps;
if (slen < 0) {
return NULL;
}
return s;
}
static int
{
if (!xfer->blksize_from_opt) {
return 0;
}
}
static int
{
if (!xfer->timeout_from_opt) {
return 0;
}
}
static int
{
if (!xfer->tsize_from_opt) {
return 0;
}
/* XXX: FIXME: want 64 bit */
}
static int
{
int sz;
return -1;
}
++sz; /* for nul byte */
return -1;
}
++sz; /* for nul byte */
return 0;
}
static ssize_t
{
void *end;
return -1;
}
}
static int
{
if (report_transient_errors) {
}
return -1;
}
/**
* Send an error packet to the peer.
*
* PCB may not be created yet in which case send the error packet from
* the TFTP server port (*).
*
* (*) We delay creation of the PCB in response to the original
* request until the request is verified and accepted. This makes
* using tcpdump(8) easier, since tcpdump does not track TFTP
* transfers, so an error reply from a new PCB is not recognized as
* such and is not decoded as TFTP.
*
* Always returns -1 for callers to reuse.
*/
static int
{
struct pbuf *q;
if (q == NULL) {
return -1;
}
}
else {
}
pbuf_free(q);
return -1;
}
/**
* Send an error packet from TFTP server port to the specified peer.
*/
static void
{
struct pbuf *q;
if (q != NULL) {
pbuf_free(q);
}
}
/**
* Create ERROR pbuf with formatted error message.
*/
static struct pbuf *
{
struct tftp_error {
char errmsg[512];
};
struct pbuf *p;
struct tftp_error *errpkt;
int msgsz;
if (p == NULL) {
return NULL;
}
if (msgsz < 0) {
msgsz = 1;
}
++msgsz; /* for nul byte */
}
else {
}
return p;
}