Reformat the source code

The source code was a mix of different styles; normalize on NASM
style; basically K&R style with 4 space indentation.
This commit is contained in:
H. Peter Anvin 2008-07-08 17:14:44 -04:00
parent 62533e7441
commit 22accddda0
18 changed files with 2794 additions and 2820 deletions

View file

@ -41,8 +41,7 @@
#ifndef lint #ifndef lint
/* static char sccsid[] = "@(#)tftpsubs.c 8.1 (Berkeley) 6/6/93"; */ /* static char sccsid[] = "@(#)tftpsubs.c 8.1 (Berkeley) 6/6/93"; */
/* static char rcsid[] = "$OpenBSD: tftpsubs.c,v 1.2 1996/06/26 05:40:36 deraadt Exp $"; */ /* static char rcsid[] = "$OpenBSD: tftpsubs.c,v 1.2 1996/06/26 05:40:36 deraadt Exp $"; */
static const char *rcsid UNUSED = static const char *rcsid UNUSED = "tftp-hpa: $Id$";
"tftp-hpa: $Id$";
#endif /* not lint */ #endif /* not lint */
/* Simple minded read-ahead/write-behind subroutines for tftp user and /* Simple minded read-ahead/write-behind subroutines for tftp user and
@ -81,13 +80,19 @@ int prevchar = -1; /* putbuf: previous char (cr check) */
static struct tftphdr *rw_init(int); static struct tftphdr *rw_init(int);
struct tftphdr *w_init() { return rw_init(0); } /* write-behind */ struct tftphdr *w_init()
struct tftphdr *r_init() { return rw_init(1); } /* read-ahead */ {
return rw_init(0);
} /* write-behind */
struct tftphdr *r_init()
{
return rw_init(1);
} /* read-ahead */
/* init for either read-ahead or write-behind */ /* init for either read-ahead or write-behind */
/* x == zero for write-behind, one for read-head */ /* x == zero for write-behind, one for read-head */
static struct tftphdr * static struct tftphdr *rw_init(int x)
rw_init(int x)
{ {
newline = 0; /* init crlf flag */ newline = 0; /* init crlf flag */
prevchar = -1; prevchar = -1;
@ -98,12 +103,10 @@ rw_init(int x)
return (struct tftphdr *)bfs[0].buf; return (struct tftphdr *)bfs[0].buf;
} }
/* Have emptied current buffer by sending to net and getting ack. /* Have emptied current buffer by sending to net and getting ack.
Free it and return next buffer filled with data. Free it and return next buffer filled with data.
*/ */
int int readit(FILE * file, struct tftphdr **dpp, int convert)
readit(FILE *file, struct tftphdr **dpp, int convert)
{ {
struct bf *b; struct bf *b;
@ -113,7 +116,7 @@ readit(FILE *file, struct tftphdr **dpp, int convert)
b = &bfs[current]; /* look at new buffer */ b = &bfs[current]; /* look at new buffer */
if (b->counter == BF_FREE) /* if it's empty */ if (b->counter == BF_FREE) /* if it's empty */
read_ahead(file, convert); /* fill it */ read_ahead(file, convert); /* fill it */
/* assert(b->counter != BF_FREE);*//* check */ /* assert(b->counter != BF_FREE);*//* check */
*dpp = (struct tftphdr *)b->buf; /* set caller's ptr */ *dpp = (struct tftphdr *)b->buf; /* set caller's ptr */
return b->counter; return b->counter;
} }
@ -122,8 +125,7 @@ readit(FILE *file, struct tftphdr **dpp, int convert)
* fill the input buffer, doing ascii conversions if requested * fill the input buffer, doing ascii conversions if requested
* conversions are lf -> cr,lf and cr -> cr, nul * conversions are lf -> cr,lf and cr -> cr, nul
*/ */
void void read_ahead(FILE * file, int convert)
read_ahead(FILE *file, int convert)
{ {
int i; int i;
char *p; char *p;
@ -144,16 +146,17 @@ read_ahead(FILE *file, int convert)
} }
p = dp->th_data; p = dp->th_data;
for (i = 0 ; i < segsize; i++) { for (i = 0; i < segsize; i++) {
if (newline) { if (newline) {
if (prevchar == '\n') if (prevchar == '\n')
c = '\n'; /* lf to cr,lf */ c = '\n'; /* lf to cr,lf */
else c = '\0'; /* cr to cr,nul */ else
c = '\0'; /* cr to cr,nul */
newline = 0; newline = 0;
} } else {
else {
c = getc(file); c = getc(file);
if (c == EOF) break; if (c == EOF)
break;
if (c == '\n' || c == '\r') { if (c == '\n' || c == '\r') {
prevchar = c; prevchar = c;
c = '\r'; c = '\r';
@ -169,8 +172,7 @@ read_ahead(FILE *file, int convert)
from the queue. Calls write_behind only if next buffer not from the queue. Calls write_behind only if next buffer not
available. available.
*/ */
int int writeit(FILE * file, struct tftphdr **dpp, int ct, int convert)
writeit(FILE *file, struct tftphdr **dpp, int ct, int convert)
{ {
bfs[current].counter = ct; /* set size of data to write */ bfs[current].counter = ct; /* set size of data to write */
current = !current; /* switch to other buffer */ current = !current; /* switch to other buffer */
@ -187,8 +189,7 @@ writeit(FILE *file, struct tftphdr **dpp, int ct, int convert)
* Note spec is undefined if we get CR as last byte of file or a * 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. * CR followed by anything else. In this case we leave it alone.
*/ */
int int write_behind(FILE * file, int convert)
write_behind(FILE *file, int convert)
{ {
char *buf; char *buf;
int count; int count;
@ -208,7 +209,8 @@ write_behind(FILE *file, int convert)
nextone = !nextone; /* incr for next time */ nextone = !nextone; /* incr for next time */
buf = dp->th_data; buf = dp->th_data;
if (count <= 0) return -1; /* nak logic? */ if (count <= 0)
return -1; /* nak logic? */
if (convert == 0) if (convert == 0)
return write(fileno(file), buf, count); return write(fileno(file), buf, count);
@ -220,19 +222,17 @@ write_behind(FILE *file, int convert)
if (prevchar == '\r') { /* if prev char was cr */ if (prevchar == '\r') { /* if prev char was cr */
if (c == '\n') /* if have cr,lf then just */ if (c == '\n') /* if have cr,lf then just */
fseek(file, -1, 1); /* smash lf on top of the cr */ fseek(file, -1, 1); /* smash lf on top of the cr */
else else if (c == '\0') /* if have cr,nul then */
if (c == '\0') /* if have cr,nul then */
goto skipit; /* just skip over the putc */ goto skipit; /* just skip over the putc */
/* else just fall through and allow it */ /* else just fall through and allow it */
} }
putc(c, file); putc(c, file);
skipit: skipit:
prevchar = c; prevchar = c;
} }
return count; return count;
} }
/* When an error has occurred, it is possible that the two sides /* 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 * 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. * response to packet N is really their response to packet N-1.
@ -244,9 +244,8 @@ skipit:
* when trace is active). * when trace is active).
*/ */
int int synchnet(int f)
synchnet(int f) /* socket to flush */ { /* socket to flush */
{
int pktcount = 0; int pktcount = 0;
char rbuf[PKTSIZE]; char rbuf[PKTSIZE];
struct sockaddr_in from; struct sockaddr_in from;
@ -254,27 +253,28 @@ synchnet(int f) /* socket to flush */
fd_set socketset; fd_set socketset;
struct timeval notime; struct timeval notime;
while ( 1 ) { while (1) {
notime.tv_sec = notime.tv_usec = 0; notime.tv_sec = notime.tv_usec = 0;
FD_ZERO(&socketset); FD_ZERO(&socketset);
FD_SET(f, &socketset); FD_SET(f, &socketset);
if ( select(f, &socketset, NULL, NULL, &notime) <= 0 ) if (select(f, &socketset, NULL, NULL, &notime) <= 0)
break; /* Nothing to read */ break; /* Nothing to read */
/* Otherwise drain the packet */ /* Otherwise drain the packet */
pktcount++; pktcount++;
fromlen = sizeof from; fromlen = sizeof from;
(void) recvfrom(f, rbuf, sizeof (rbuf), 0, (void)recvfrom(f, rbuf, sizeof(rbuf), 0,
(struct sockaddr *)&from, &fromlen); (struct sockaddr *)&from, &fromlen);
} }
return pktcount; /* Return packets drained */ return pktcount; /* Return packets drained */
} }
int pick_port_bind(int sockfd, struct sockaddr_in *myaddr,
int pick_port_bind(int sockfd, struct sockaddr_in *myaddr, unsigned int port_range_from, unsigned int port_range_to) unsigned int port_range_from,
unsigned int port_range_to)
{ {
unsigned int port, firstport; unsigned int port, firstport;
int port_range = 0; int port_range = 0;
@ -284,7 +284,7 @@ int pick_port_bind(int sockfd, struct sockaddr_in *myaddr, unsigned int port_ran
} }
firstport = port_range firstport = port_range
? port_range_from + rand() % (port_range_to-port_range_from+1) ? port_range_from + rand() % (port_range_to - port_range_from + 1)
: 0; : 0;
port = firstport; port = firstport;
@ -294,7 +294,7 @@ int pick_port_bind(int sockfd, struct sockaddr_in *myaddr, unsigned int port_ran
if (bind(sockfd, (struct sockaddr *)myaddr, sizeof *myaddr) < 0) { if (bind(sockfd, (struct sockaddr *)myaddr, sizeof *myaddr) < 0) {
/* Some versions of Linux return EINVAL instead of EADDRINUSE */ /* Some versions of Linux return EINVAL instead of EADDRINUSE */
if ( !(port_range && (errno == EINVAL || errno == EADDRINUSE)) ) if (!(port_range && (errno == EINVAL || errno == EADDRINUSE)))
return -1; return -1;
/* Normally, we shouldn't have to loop, but some situations involving /* Normally, we shouldn't have to loop, but some situations involving
@ -304,9 +304,9 @@ int pick_port_bind(int sockfd, struct sockaddr_in *myaddr, unsigned int port_ran
} }
port++; port++;
if ( port > port_range_to ) if (port > port_range_to)
port = port_range_from; port = port_range_from;
} while ( port != firstport ); } while (port != firstport);
return -1; return -1;
} }

View file

@ -62,6 +62,7 @@ int writeit(FILE *, struct tftphdr **, int, int);
extern int segsize; extern int segsize;
#define MAX_SEGSIZE 65464 #define MAX_SEGSIZE 65464
int pick_port_bind(int sockfd, struct sockaddr_in *myaddr, unsigned int from, unsigned int to); int pick_port_bind(int sockfd, struct sockaddr_in *myaddr,
unsigned int from, unsigned int to);
#endif #endif

View file

@ -293,7 +293,7 @@ void *xmalloc(size_t);
char *xstrdup(const char *); char *xstrdup(const char *);
#ifndef HAVE_BSD_SIGNAL #ifndef HAVE_BSD_SIGNAL
void (*bsd_signal(int, void (*)(int)))(int); void (*bsd_signal(int, void (*)(int))) (int);
#endif #endif
#ifndef HAVE_DUP2 #ifndef HAVE_DUP2
int dup2(int, int); int dup2(int, int);

View file

@ -6,8 +6,7 @@
#include "config.h" #include "config.h"
void (*bsd_signal(int signum, void (*handler)(int)))(int) void (*bsd_signal(int signum, void (*handler) (int))) (int) {
{
struct sigaction action, oldaction; struct sigaction action, oldaction;
memset(&action, 0, sizeof action); memset(&action, 0, sizeof action);

View file

@ -17,8 +17,7 @@ int daemon(int nochdir, int noclose)
if (!noclose) { if (!noclose) {
if ((nullfd = open("/dev/null", O_RDWR)) < 0 || if ((nullfd = open("/dev/null", O_RDWR)) < 0 ||
dup2(nullfd, 0) < 0 || dup2(nullfd, 0) < 0 ||
dup2(nullfd, 1) < 0 || dup2(nullfd, 1) < 0 || dup2(nullfd, 2) < 0)
dup2(nullfd, 2) < 0)
return -1; return -1;
close(nullfd); close(nullfd);
} }

View file

@ -21,5 +21,3 @@ int dup2(int oldfd, int newfd)
return rv; return rv;
} }

View file

@ -11,7 +11,7 @@ void *xmalloc(size_t size)
{ {
void *p = malloc(size); void *p = malloc(size);
if ( !p ) { if (!p) {
fprintf(stderr, "Out of memory!\n"); fprintf(stderr, "Out of memory!\n");
exit(128); exit(128);
} }

View file

@ -11,7 +11,7 @@ char *xstrdup(const char *s)
{ {
char *p = strdup(s); char *p = strdup(s);
if ( !p ) { if (!p) {
fprintf(stderr, "Out of memory!\n"); fprintf(stderr, "Out of memory!\n");
exit(128); exit(128);
} }

View file

@ -41,7 +41,7 @@
#ifndef RECVFILE_H #ifndef RECVFILE_H
#define RECVFILE_H #define RECVFILE_H
void tftp_recvfile (int, const char *, const char *); void tftp_recvfile(int, const char *, const char *);
void tftp_sendfile (int, const char *, const char *); void tftp_sendfile(int, const char *, const char *);
#endif #endif

View file

@ -37,13 +37,11 @@
#include "common/tftpsubs.h" #include "common/tftpsubs.h"
#ifndef lint #ifndef lint
static const char *copyright UNUSED = static const char *copyright UNUSED = "@(#) Copyright (c) 1983, 1993\n\
"@(#) Copyright (c) 1983, 1993\n\
The Regents of the University of California. All rights reserved.\n"; The Regents of the University of California. All rights reserved.\n";
/* static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93"; */ /* static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93"; */
/* static char rcsid[] = "$OpenBSD: main.c,v 1.4 1997/01/17 07:13:30 millert Exp $"; */ /* static char rcsid[] = "$OpenBSD: main.c,v 1.4 1997/01/17 07:13:30 millert Exp $"; */
static const char *rcsid UNUSED = static const char *rcsid UNUSED = "tftp-hpa $Id$";
"tftp-hpa $Id$";
#endif /* not lint */ #endif /* not lint */
/* Many bug fixes are from Jim Guyton <guyton@rand-unix> */ /* Many bug fixes are from Jim Guyton <guyton@rand-unix> */
@ -73,13 +71,14 @@ struct modes {
}; };
static const struct modes modes[] = { static const struct modes modes[] = {
{ "netascii", "netascii", O_TEXT }, {"netascii", "netascii", O_TEXT},
{ "ascii", "netascii", O_TEXT }, {"ascii", "netascii", O_TEXT},
{ "octet", "octet", O_BINARY }, {"octet", "octet", O_BINARY},
{ "binary", "octet", O_BINARY }, {"binary", "octet", O_BINARY},
{ "image", "octet", O_BINARY }, {"image", "octet", O_BINARY},
{ 0, 0, 0 } {0, 0, 0}
}; };
#define MODE_OCTET (&modes[2]) #define MODE_OCTET (&modes[2])
#define MODE_NETASCII (&modes[0]) #define MODE_NETASCII (&modes[0])
#define MODE_DEFAULT MODE_NETASCII #define MODE_DEFAULT MODE_NETASCII
@ -107,27 +106,27 @@ int portrange = 0;
unsigned int portrange_from = 0; unsigned int portrange_from = 0;
unsigned int portrange_to = 0; unsigned int portrange_to = 0;
void get (int, char **); void get(int, char **);
void help (int, char **); void help(int, char **);
void modecmd (int, char **); void modecmd(int, char **);
void put (int, char **); void put(int, char **);
void quit (int, char **); void quit(int, char **);
void setascii (int, char **); void setascii(int, char **);
void setbinary (int, char **); void setbinary(int, char **);
void setpeer (int, char **); void setpeer(int, char **);
void setrexmt (int, char **); void setrexmt(int, char **);
void settimeout (int, char **); void settimeout(int, char **);
void settrace (int, char **); void settrace(int, char **);
void setverbose (int, char **); void setverbose(int, char **);
void status (int, char **); void status(int, char **);
void setliteral (int, char **); void setliteral(int, char **);
static void command (void); static void command(void);
static void getusage (char *); static void getusage(char *);
static void makeargv (void); static void makeargv(void);
static void putusage (char *); static void putusage(char *);
static void settftpmode (const struct modes *); static void settftpmode(const struct modes *);
#define HELPINDENT (sizeof("connect")) #define HELPINDENT (sizeof("connect"))
@ -138,52 +137,52 @@ struct cmd {
}; };
struct cmd cmdtab[] = { struct cmd cmdtab[] = {
{ "connect", {"connect",
"connect to remote tftp", "connect to remote tftp",
setpeer }, setpeer},
{ "mode", {"mode",
"set file transfer mode", "set file transfer mode",
modecmd }, modecmd},
{ "put", {"put",
"send file", "send file",
put }, put},
{ "get", {"get",
"receive file", "receive file",
get }, get},
{ "quit", {"quit",
"exit tftp", "exit tftp",
quit }, quit},
{ "verbose", {"verbose",
"toggle verbose mode", "toggle verbose mode",
setverbose }, setverbose},
{ "trace", {"trace",
"toggle packet tracing", "toggle packet tracing",
settrace }, settrace},
{ "literal", {"literal",
"toggle literal mode, ignore ':' in file name", "toggle literal mode, ignore ':' in file name",
setliteral }, setliteral},
{ "status", {"status",
"show current status", "show current status",
status }, status},
{ "binary", {"binary",
"set mode to octet", "set mode to octet",
setbinary }, setbinary},
{ "ascii", {"ascii",
"set mode to netascii", "set mode to netascii",
setascii }, setascii},
{ "rexmt", {"rexmt",
"set per-packet transmission timeout", "set per-packet transmission timeout",
setrexmt }, setrexmt},
{ "timeout", {"timeout",
"set total retransmission timeout", "set total retransmission timeout",
settimeout }, settimeout},
{ "?", {"?",
"print help information", "print help information",
help }, help},
{ "help", {"help",
"print help information", "print help information",
help }, help},
{ 0, 0, 0 } {0, 0, 0}
}; };
struct cmd *getcmd(char *); struct cmd *getcmd(char *);
@ -195,12 +194,13 @@ const char *program;
static inline void usage(int errcode) static inline void usage(int errcode)
{ {
fprintf(stderr, "Usage: %s [-v][-l][-m mode] [host [port]] [-c command]\n", program); fprintf(stderr,
"Usage: %s [-v][-l][-m mode] [host [port]] [-c command]\n",
program);
exit(errcode); exit(errcode);
} }
int int main(int argc, char *argv[])
main(int argc, char *argv[])
{ {
struct sockaddr_in s_in; struct sockaddr_in s_in;
int arg; int arg;
@ -217,10 +217,10 @@ main(int argc, char *argv[])
peerargv[0] = argv[0]; peerargv[0] = argv[0];
peerargc = 1; peerargc = 1;
for ( arg = 1 ; !iscmd && arg < argc ; arg++ ) { for (arg = 1; !iscmd && arg < argc; arg++) {
if ( argv[arg][0] == '-' ) { if (argv[arg][0] == '-') {
for ( optx = &argv[arg][1] ; *optx ; optx++ ) { for (optx = &argv[arg][1]; *optx; optx++) {
switch ( *optx ) { switch (*optx) {
case 'v': case 'v':
verbose = 1; verbose = 1;
break; break;
@ -232,19 +232,20 @@ main(int argc, char *argv[])
literal = 1; literal = 1;
break; break;
case 'm': case 'm':
if ( ++arg >= argc ) if (++arg >= argc)
usage(EX_USAGE); usage(EX_USAGE);
{ {
const struct modes *p; const struct modes *p;
for ( p = modes ; p->m_name ; p++ ) { for (p = modes; p->m_name; p++) {
if (!strcmp(argv[arg], p->m_name)) if (!strcmp(argv[arg], p->m_name))
break; break;
} }
if (p->m_name) { if (p->m_name) {
settftpmode(p); settftpmode(p);
} else { } else {
fprintf(stderr, "%s: invalid mode: %s\n", argv[0], argv[arg]); fprintf(stderr, "%s: invalid mode: %s\n",
argv[0], argv[arg]);
exit(EX_USAGE); exit(EX_USAGE);
} }
} }
@ -253,10 +254,13 @@ main(int argc, char *argv[])
iscmd = 1; iscmd = 1;
break; break;
case 'R': case 'R':
if ( ++arg >= argc ) if (++arg >= argc)
usage(EX_USAGE); usage(EX_USAGE);
if ( sscanf(argv[arg], "%u:%u", &portrange_from, &portrange_to) != 2 || if (sscanf
portrange_from > portrange_to || portrange_to > 65535 ) { (argv[arg], "%u:%u", &portrange_from,
&portrange_to) != 2
|| portrange_from > portrange_to
|| portrange_to > 65535) {
fprintf(stderr, "Bad port range: %s\n", argv[arg]); fprintf(stderr, "Bad port range: %s\n", argv[arg]);
exit(EX_USAGE); exit(EX_USAGE);
} }
@ -268,7 +272,7 @@ main(int argc, char *argv[])
} }
} }
} else { } else {
if ( peerargc >= 3 ) if (peerargc >= 3)
usage(EX_USAGE); usage(EX_USAGE);
peerargv[peerargc++] = argv[arg]; peerargv[peerargc++] = argv[arg];
@ -282,7 +286,8 @@ main(int argc, char *argv[])
if (sp == 0) { if (sp == 0) {
/* Use canned values */ /* Use canned values */
if (verbose) if (verbose)
fprintf(stderr, "tftp: tftp/udp: unknown service, faking it...\n"); fprintf(stderr,
"tftp: tftp/udp: unknown service, faking it...\n");
sp = xmalloc(sizeof(struct servent)); sp = xmalloc(sizeof(struct servent));
sp->s_name = (char *)"tftp"; sp->s_name = (char *)"tftp";
sp->s_aliases = NULL; sp->s_aliases = NULL;
@ -295,7 +300,7 @@ main(int argc, char *argv[])
perror("tftp: socket"); perror("tftp: socket");
exit(EX_OSERR); exit(EX_OSERR);
} }
bzero((char *)&s_in, sizeof (s_in)); bzero((char *)&s_in, sizeof(s_in));
s_in.sin_family = AF_INET; s_in.sin_family = AF_INET;
if (pick_port_bind(f, &s_in, portrange_from, portrange_to)) { if (pick_port_bind(f, &s_in, portrange_from, portrange_to)) {
perror("tftp: bind"); perror("tftp: bind");
@ -303,36 +308,36 @@ main(int argc, char *argv[])
} }
bsd_signal(SIGINT, intr); bsd_signal(SIGINT, intr);
if ( peerargc ) { if (peerargc) {
/* Set peer */ /* Set peer */
if (sigsetjmp(toplevel,1) != 0) if (sigsetjmp(toplevel, 1) != 0)
exit(EX_NOHOST); exit(EX_NOHOST);
setpeer(peerargc, peerargv); setpeer(peerargc, peerargv);
} }
if ( iscmd && pargc ) { if (iscmd && pargc) {
/* -c specified; execute command and exit */ /* -c specified; execute command and exit */
struct cmd *c; struct cmd *c;
if (sigsetjmp(toplevel,1) != 0) if (sigsetjmp(toplevel, 1) != 0)
exit(EX_UNAVAILABLE); exit(EX_UNAVAILABLE);
c = getcmd(pargv[0]); c = getcmd(pargv[0]);
if ( c == (struct cmd *)-1 || c == (struct cmd *)0 ) { if (c == (struct cmd *)-1 || c == (struct cmd *)0) {
fprintf(stderr, "%s: invalid command: %s\n", argv[0], pargv[1]); fprintf(stderr, "%s: invalid command: %s\n", argv[0],
pargv[1]);
exit(EX_USAGE); exit(EX_USAGE);
} }
(*c->handler)(pargc, pargv); (*c->handler) (pargc, pargv);
exit(0); exit(0);
} }
#ifdef WITH_READLINE #ifdef WITH_READLINE
#ifdef HAVE_READLINE_HISTORY_H #ifdef HAVE_READLINE_HISTORY_H
using_history(); using_history();
#endif #endif
#endif #endif
if (sigsetjmp(toplevel,1) != 0) if (sigsetjmp(toplevel, 1) != 0)
(void)putchar('\n'); (void)putchar('\n');
command(); command();
@ -343,8 +348,7 @@ char *hostname;
/* Called when a command is incomplete; modifies /* Called when a command is incomplete; modifies
the global variable "line" */ the global variable "line" */
static void static void getmoreargs(const char *partial, const char *mprompt)
getmoreargs(const char *partial, const char *mprompt)
{ {
#ifdef WITH_READLINE #ifdef WITH_READLINE
char *eline; char *eline;
@ -361,9 +365,9 @@ getmoreargs(const char *partial, const char *mprompt)
free(line); free(line);
line = NULL; line = NULL;
} }
line = xmalloc(len+elen+1); line = xmalloc(len + elen + 1);
strcpy(line, partial); strcpy(line, partial);
strcpy(line+len, eline); strcpy(line + len, eline);
free(eline); free(eline);
#ifdef HAVE_READLINE_HISTORY_H #ifdef HAVE_READLINE_HISTORY_H
@ -374,14 +378,13 @@ getmoreargs(const char *partial, const char *mprompt)
strcpy(line, partial); strcpy(line, partial);
fputs(mprompt, stdout); fputs(mprompt, stdout);
if ( fgets(line+len, LBUFLEN-len, stdin) == 0 ) if (fgets(line + len, LBUFLEN - len, stdin) == 0)
if ( feof(stdin) ) if (feof(stdin))
exit(0); /* EOF */ exit(0); /* EOF */
#endif #endif
} }
void void setpeer(int argc, char *argv[])
setpeer(int argc, char *argv[])
{ {
struct hostent *host; struct hostent *host;
@ -410,7 +413,7 @@ setpeer(int argc, char *argv[])
if (argc == 3) { if (argc == 3) {
struct servent *usp; struct servent *usp;
usp = getservbyname(argv[2], "udp"); usp = getservbyname(argv[2], "udp");
if ( usp ) { if (usp) {
port = usp->s_port; port = usp->s_port;
} else { } else {
unsigned long myport; unsigned long myport;
@ -421,7 +424,7 @@ setpeer(int argc, char *argv[])
connected = 0; connected = 0;
return; return;
} }
port = htons((u_short)myport); port = htons((u_short) myport);
} }
} }
@ -433,8 +436,7 @@ setpeer(int argc, char *argv[])
connected = 1; connected = 1;
} }
void void modecmd(int argc, char *argv[])
modecmd(int argc, char *argv[])
{ {
const struct modes *p; const struct modes *p;
const char *sep; const char *sep;
@ -466,34 +468,31 @@ modecmd(int argc, char *argv[])
return; return;
} }
void void setbinary(int argc, char *argv[])
setbinary(int argc, char *argv[])
{ {
(void)argc; (void)argv; /* Quiet unused warning */ (void)argc;
(void)argv; /* Quiet unused warning */
settftpmode(MODE_OCTET); settftpmode(MODE_OCTET);
} }
void void setascii(int argc, char *argv[])
setascii(int argc, char *argv[])
{ {
(void)argc; (void)argv; /* Quiet unused warning */ (void)argc;
(void)argv; /* Quiet unused warning */
settftpmode(MODE_NETASCII); settftpmode(MODE_NETASCII);
} }
static void static void settftpmode(const struct modes *newmode)
settftpmode(const struct modes *newmode)
{ {
mode = newmode; mode = newmode;
if (verbose) if (verbose)
printf("mode set to %s\n", mode->m_mode); printf("mode set to %s\n", mode->m_mode);
} }
/* /*
* Send file(s). * Send file(s).
*/ */
void void put(int argc, char *argv[])
put(int argc, char *argv[])
{ {
int fd; int fd;
int n; int n;
@ -538,9 +537,10 @@ put(int argc, char *argv[])
} }
if (argc < 4) { if (argc < 4) {
cp = argc == 2 ? tail(targ) : argv[1]; cp = argc == 2 ? tail(targ) : argv[1];
fd = open(cp, O_RDONLY|mode->m_openflags); fd = open(cp, O_RDONLY | mode->m_openflags);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "tftp: "); perror(cp); fprintf(stderr, "tftp: ");
perror(cp);
return; return;
} }
if (verbose) if (verbose)
@ -556,9 +556,10 @@ put(int argc, char *argv[])
*cp++ = '/'; *cp++ = '/';
for (n = 1; n < argc - 1; n++) { for (n = 1; n < argc - 1; n++) {
strcpy(cp, tail(argv[n])); strcpy(cp, tail(argv[n]));
fd = open(argv[n], O_RDONLY|mode->m_openflags); fd = open(argv[n], O_RDONLY | mode->m_openflags);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "tftp: "); perror(argv[n]); fprintf(stderr, "tftp: ");
perror(argv[n]);
continue; continue;
} }
if (verbose) if (verbose)
@ -569,8 +570,7 @@ put(int argc, char *argv[])
} }
} }
static void static void putusage(char *s)
putusage(char *s)
{ {
printf("usage: %s file ... host:target, or\n", s); printf("usage: %s file ... host:target, or\n", s);
printf(" %s file ... target (when already connected)\n", s); printf(" %s file ... target (when already connected)\n", s);
@ -579,8 +579,7 @@ putusage(char *s)
/* /*
* Receive file(s). * Receive file(s).
*/ */
void void get(int argc, char *argv[])
get(int argc, char *argv[])
{ {
int fd; int fd;
int n; int n;
@ -598,13 +597,13 @@ get(int argc, char *argv[])
return; return;
} }
if (!connected) { if (!connected) {
for (n = 1; n < argc ; n++) for (n = 1; n < argc; n++)
if (literal || strchr(argv[n], ':') == 0) { if (literal || strchr(argv[n], ':') == 0) {
getusage(argv[0]); getusage(argv[0]);
return; return;
} }
} }
for (n = 1; n < argc ; n++) { for (n = 1; n < argc; n++) {
src = strchr(argv[n], ':'); src = strchr(argv[n], ':');
if (literal || src == NULL) if (literal || src == NULL)
src = argv[n]; src = argv[n];
@ -618,17 +617,18 @@ get(int argc, char *argv[])
herror((char *)NULL); herror((char *)NULL);
continue; continue;
} }
bcopy(hp->h_addr, (caddr_t)&peeraddr.sin_addr, bcopy(hp->h_addr, (caddr_t) & peeraddr.sin_addr, hp->h_length);
hp->h_length);
peeraddr.sin_family = hp->h_addrtype; peeraddr.sin_family = hp->h_addrtype;
connected = 1; connected = 1;
hostname = xstrdup(hp->h_name); hostname = xstrdup(hp->h_name);
} }
if (argc < 4) { if (argc < 4) {
cp = argc == 3 ? argv[2] : tail(src); cp = argc == 3 ? argv[2] : tail(src);
fd = open(cp, O_WRONLY|O_CREAT|O_TRUNC|mode->m_openflags, 0666); fd = open(cp, O_WRONLY | O_CREAT | O_TRUNC | mode->m_openflags,
0666);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "tftp: "); perror(cp); fprintf(stderr, "tftp: ");
perror(cp);
return; return;
} }
if (verbose) if (verbose)
@ -639,9 +639,11 @@ get(int argc, char *argv[])
break; break;
} }
cp = tail(src); /* new .. jdg */ cp = tail(src); /* new .. jdg */
fd = open(cp, O_WRONLY|O_CREAT|O_TRUNC|mode->m_openflags, 0666); fd = open(cp, O_WRONLY | O_CREAT | O_TRUNC | mode->m_openflags,
0666);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "tftp: "); perror(cp); fprintf(stderr, "tftp: ");
perror(cp);
continue; continue;
} }
if (verbose) if (verbose)
@ -652,8 +654,7 @@ get(int argc, char *argv[])
} }
} }
static void static void getusage(char *s)
getusage(char *s)
{ {
printf("usage: %s host:file host:file ... file, or\n", s); printf("usage: %s host:file host:file ... file, or\n", s);
printf(" %s file file ... file if connected\n", s); printf(" %s file file ... file if connected\n", s);
@ -661,8 +662,7 @@ getusage(char *s)
int rexmtval = TIMEOUT; int rexmtval = TIMEOUT;
void void setrexmt(int argc, char *argv[])
setrexmt(int argc, char *argv[])
{ {
int t; int t;
@ -685,8 +685,7 @@ setrexmt(int argc, char *argv[])
int maxtimeout = 5 * TIMEOUT; int maxtimeout = 5 * TIMEOUT;
void void settimeout(int argc, char *argv[])
settimeout(int argc, char *argv[])
{ {
int t; int t;
@ -707,30 +706,30 @@ settimeout(int argc, char *argv[])
maxtimeout = t; maxtimeout = t;
} }
void void setliteral(int argc, char *argv[])
setliteral(int argc, char *argv[])
{ {
(void)argc; (void)argv; /* Quiet unused warning */ (void)argc;
(void)argv; /* Quiet unused warning */
literal = !literal; literal = !literal;
printf("Literal mode %s.\n", literal ? "on" : "off"); printf("Literal mode %s.\n", literal ? "on" : "off");
} }
void void status(int argc, char *argv[])
status(int argc, char *argv[])
{ {
(void)argc; (void)argv; /* Quiet unused warning */ (void)argc;
(void)argv; /* Quiet unused warning */
if (connected) if (connected)
printf("Connected to %s.\n", hostname); printf("Connected to %s.\n", hostname);
else else
printf("Not connected.\n"); printf("Not connected.\n");
printf("Mode: %s Verbose: %s Tracing: %s Literal: %s\n", mode->m_mode, printf("Mode: %s Verbose: %s Tracing: %s Literal: %s\n", mode->m_mode,
verbose ? "on" : "off", trace ? "on" : "off", literal ? "on" : "off"); verbose ? "on" : "off", trace ? "on" : "off",
literal ? "on" : "off");
printf("Rexmt-interval: %d seconds, Max-timeout: %d seconds\n", printf("Rexmt-interval: %d seconds, Max-timeout: %d seconds\n",
rexmtval, maxtimeout); rexmtval, maxtimeout);
} }
void void intr(int sig)
intr(int sig)
{ {
(void)sig; /* Quiet unused warning */ (void)sig; /* Quiet unused warning */
@ -739,8 +738,7 @@ intr(int sig)
siglongjmp(toplevel, -1); siglongjmp(toplevel, -1);
} }
char * char *tail(char *filename)
tail(char *filename)
{ {
char *s; char *s;
@ -758,19 +756,18 @@ tail(char *filename)
/* /*
* Command parser. * Command parser.
*/ */
static void static void command(void)
command(void)
{ {
struct cmd *c; struct cmd *c;
for (;;) { for (;;) {
#ifdef WITH_READLINE #ifdef WITH_READLINE
if ( line ) { if (line) {
free(line); free(line);
line = NULL; line = NULL;
} }
line = readline(prompt); line = readline(prompt);
if ( !line ) if (!line)
exit(0); /* EOF */ exit(0); /* EOF */
#else #else
fputs(prompt, stdout); fputs(prompt, stdout);
@ -802,12 +799,11 @@ command(void)
printf("?Invalid command\n"); printf("?Invalid command\n");
continue; continue;
} }
(*c->handler)(margc, margv); (*c->handler) (margc, margv);
} }
} }
struct cmd * struct cmd *getcmd(char *name)
getcmd(char *name)
{ {
const char *p; const char *p;
char *q; char *q;
@ -838,8 +834,7 @@ getcmd(char *name)
/* /*
* Slice a string up into argc/argv. * Slice a string up into argc/argv.
*/ */
static void static void makeargv(void)
makeargv(void)
{ {
char *cp; char *cp;
char **argp = margv; char **argp = margv;
@ -861,18 +856,17 @@ makeargv(void)
*argp++ = 0; *argp++ = 0;
} }
void void quit(int argc, char *argv[])
quit(int argc, char *argv[])
{ {
(void)argc; (void)argv; /* Quiet unused warning */ (void)argc;
(void)argv; /* Quiet unused warning */
exit(0); exit(0);
} }
/* /*
* Help command. * Help command.
*/ */
void void help(int argc, char *argv[])
help(int argc, char *argv[])
{ {
struct cmd *c; struct cmd *c;
@ -897,19 +891,19 @@ help(int argc, char *argv[])
} }
} }
void void settrace(int argc, char *argv[])
settrace(int argc, char *argv[])
{ {
(void)argc; (void)argv; /* Quiet unused warning */ (void)argc;
(void)argv; /* Quiet unused warning */
trace = !trace; trace = !trace;
printf("Packet tracing %s.\n", trace ? "on" : "off"); printf("Packet tracing %s.\n", trace ? "on" : "off");
} }
void void setverbose(int argc, char *argv[])
setverbose(int argc, char *argv[])
{ {
(void)argc; (void)argv; /* Quiet unused warning */ (void)argc;
(void)argv; /* Quiet unused warning */
verbose = !verbose; verbose = !verbose;
printf("Verbose mode %s.\n", verbose ? "on" : "off"); printf("Verbose mode %s.\n", verbose ? "on" : "off");

View file

@ -39,8 +39,7 @@
#ifndef lint #ifndef lint
/* static char sccsid[] = "@(#)tftp.c 8.1 (Berkeley) 6/6/93"; */ /* static char sccsid[] = "@(#)tftp.c 8.1 (Berkeley) 6/6/93"; */
/* static char rcsid[] = "$OpenBSD: tftp.c,v 1.4 1997/08/06 06:43:45 deraadt Exp $"; */ /* static char rcsid[] = "$OpenBSD: tftp.c,v 1.4 1997/08/06 06:43:45 deraadt Exp $"; */
static const char *rcsid UNUSED = static const char *rcsid UNUSED = "tftp-hpa $Id$";
"tftp-hpa $Id$";
#endif /* not lint */ #endif /* not lint */
/* Many bug fixes are from Jim Guyton <guyton@rand-unix> */ /* Many bug fixes are from Jim Guyton <guyton@rand-unix> */
@ -74,8 +73,7 @@ static void tpacket(const char *, struct tftphdr *, int);
/* /*
* Send the requested file. * Send the requested file.
*/ */
void void tftp_sendfile(int fd, const char *name, const char *mode)
tftp_sendfile(int fd, const char *name, const char *mode)
{ {
struct tftphdr *ap; /* data and ack packets */ struct tftphdr *ap; /* data and ack packets */
struct tftphdr *dp; struct tftphdr *dp;
@ -109,11 +107,11 @@ tftp_sendfile(int fd, const char *name, const char *mode)
nak(errno + 100, NULL); nak(errno + 100, NULL);
break; break;
} }
dp->th_opcode = htons((u_short)DATA); dp->th_opcode = htons((u_short) DATA);
dp->th_block = htons((u_short)block); dp->th_block = htons((u_short) block);
} }
timeout = 0; timeout = 0;
(void) sigsetjmp(timeoutbuf,1); (void)sigsetjmp(timeoutbuf, 1);
if (trace) if (trace)
tpacket("sent", dp, size + 4); tpacket("sent", dp, size + 4);
@ -124,7 +122,7 @@ tftp_sendfile(int fd, const char *name, const char *mode)
goto abort; goto abort;
} }
read_ahead(file, convert); read_ahead(file, convert);
for ( ; ; ) { for (;;) {
alarm(rexmtval); alarm(rexmtval);
do { do {
fromlen = sizeof(from); fromlen = sizeof(from);
@ -140,11 +138,10 @@ tftp_sendfile(int fd, const char *name, const char *mode)
if (trace) if (trace)
tpacket("received", ap, n); tpacket("received", ap, n);
/* should verify packet came from server */ /* should verify packet came from server */
ap_opcode = ntohs((u_short)ap->th_opcode); ap_opcode = ntohs((u_short) ap->th_opcode);
ap_block = ntohs((u_short)ap->th_block); ap_block = ntohs((u_short) ap->th_block);
if (ap_opcode == ERROR) { if (ap_opcode == ERROR) {
printf("Error code %d: %s\n", ap_block, printf("Error code %d: %s\n", ap_block, ap->th_msg);
ap->th_msg);
goto abort; goto abort;
} }
if (ap_opcode == ACK) { if (ap_opcode == ACK) {
@ -158,8 +155,7 @@ tftp_sendfile(int fd, const char *name, const char *mode)
*/ */
j = synchnet(f); j = synchnet(f);
if (j && trace) { if (j && trace) {
printf("discarded %d packets\n", printf("discarded %d packets\n", j);
j);
} }
/* /*
* RFC1129/RFC1350: We MUST NOT re-send the DATA * RFC1129/RFC1350: We MUST NOT re-send the DATA
@ -168,12 +164,12 @@ tftp_sendfile(int fd, const char *name, const char *mode)
*/ */
} }
} }
if ( !is_request ) if (!is_request)
amount += size; amount += size;
is_request = 0; is_request = 0;
block++; block++;
} while (size == SEGSIZE || block == 1); } while (size == SEGSIZE || block == 1);
abort: abort:
fclose(file); fclose(file);
stopclock(); stopclock();
if (amount > 0) if (amount > 0)
@ -183,8 +179,7 @@ abort:
/* /*
* Receive a file. * Receive a file.
*/ */
void void tftp_recvfile(int fd, const char *name, const char *mode)
tftp_recvfile(int fd, const char *name, const char *mode)
{ {
struct tftphdr *ap; struct tftphdr *ap;
struct tftphdr *dp; struct tftphdr *dp;
@ -202,7 +197,7 @@ tftp_recvfile(int fd, const char *name, const char *mode)
dp = w_init(); dp = w_init();
ap = (struct tftphdr *)ackbuf; ap = (struct tftphdr *)ackbuf;
convert = !strcmp(mode, "netascii"); convert = !strcmp(mode, "netascii");
file = fdopen(fd, convert ?"wt":"wb"); file = fdopen(fd, convert ? "wt" : "wb");
block = 1; block = 1;
firsttrip = 1; firsttrip = 1;
amount = 0; amount = 0;
@ -213,14 +208,14 @@ tftp_recvfile(int fd, const char *name, const char *mode)
size = makerequest(RRQ, name, ap, mode); size = makerequest(RRQ, name, ap, mode);
firsttrip = 0; firsttrip = 0;
} else { } else {
ap->th_opcode = htons((u_short)ACK); ap->th_opcode = htons((u_short) ACK);
ap->th_block = htons((u_short)block); ap->th_block = htons((u_short) block);
size = 4; size = 4;
block++; block++;
} }
timeout = 0; timeout = 0;
(void) sigsetjmp(timeoutbuf,1); (void)sigsetjmp(timeoutbuf, 1);
send_ack: send_ack:
if (trace) if (trace)
tpacket("sent", ap, size); tpacket("sent", ap, size);
if (sendto(f, ackbuf, size, 0, (struct sockaddr *)&peeraddr, if (sendto(f, ackbuf, size, 0, (struct sockaddr *)&peeraddr,
@ -230,7 +225,7 @@ send_ack:
goto abort; goto abort;
} }
write_behind(file, convert); write_behind(file, convert);
for ( ; ; ) { for (;;) {
alarm(rexmtval); alarm(rexmtval);
do { do {
fromlen = sizeof(from); fromlen = sizeof(from);
@ -246,8 +241,8 @@ send_ack:
if (trace) if (trace)
tpacket("received", dp, n); tpacket("received", dp, n);
/* should verify client address */ /* should verify client address */
dp_opcode = ntohs((u_short)dp->th_opcode); dp_opcode = ntohs((u_short) dp->th_opcode);
dp_block = ntohs((u_short)dp->th_block); dp_block = ntohs((u_short) dp->th_block);
if (dp_opcode == ERROR) { if (dp_opcode == ERROR) {
printf("Error code %d: %s\n", dp_block, dp->th_msg); printf("Error code %d: %s\n", dp_block, dp->th_msg);
goto abort; goto abort;
@ -265,7 +260,7 @@ send_ack:
if (j && trace) { if (j && trace) {
printf("discarded %d packets\n", j); printf("discarded %d packets\n", j);
} }
if (dp_block == (block-1)) { if (dp_block == (block - 1)) {
goto send_ack; /* resend ack */ goto send_ack; /* resend ack */
} }
} }
@ -278,10 +273,10 @@ send_ack:
} }
amount += size; amount += size;
} while (size == SEGSIZE); } while (size == SEGSIZE);
abort: /* ok to ack, since user */ abort: /* ok to ack, since user */
ap->th_opcode = htons((u_short)ACK); /* has seen err msg */ ap->th_opcode = htons((u_short) ACK); /* has seen err msg */
ap->th_block = htons((u_short)block); ap->th_block = htons((u_short) block);
(void) sendto(f, ackbuf, 4, 0, (struct sockaddr *)&peeraddr, (void)sendto(f, ackbuf, 4, 0, (struct sockaddr *)&peeraddr,
sizeof(peeraddr)); sizeof(peeraddr));
write_behind(file, convert); /* flush last buffer */ write_behind(file, convert); /* flush last buffer */
fclose(file); fclose(file);
@ -296,8 +291,8 @@ makerequest(int request, const char *name,
{ {
char *cp; char *cp;
tp->th_opcode = htons((u_short)request); tp->th_opcode = htons((u_short) request);
cp = (char *) &(tp->th_stuff); cp = (char *)&(tp->th_stuff);
strcpy(cp, name); strcpy(cp, name);
cp += strlen(name); cp += strlen(name);
*cp++ = '\0'; *cp++ = '\0';
@ -307,8 +302,7 @@ makerequest(int request, const char *name,
return (cp - (char *)tp); return (cp - (char *)tp);
} }
static const char * const errmsgs[] = static const char *const errmsgs[] = {
{
"Undefined error code", /* 0 - EUNDEF */ "Undefined error code", /* 0 - EUNDEF */
"File not found", /* 1 - ENOTFOUND */ "File not found", /* 1 - ENOTFOUND */
"Access denied", /* 2 - EACCESS */ "Access denied", /* 2 - EACCESS */
@ -319,6 +313,7 @@ static const char * const errmsgs[] =
"No such user", /* 7 - ENOUSER */ "No such user", /* 7 - ENOUSER */
"Failure to negotiate RFC2347 options" /* 8 - EOPTNEG */ "Failure to negotiate RFC2347 options" /* 8 - EOPTNEG */
}; };
#define ERR_CNT (sizeof(errmsgs)/sizeof(const char *)) #define ERR_CNT (sizeof(errmsgs)/sizeof(const char *))
/* /*
@ -327,32 +322,31 @@ static const char * const errmsgs[] =
* standard TFTP codes, or a UNIX errno * standard TFTP codes, or a UNIX errno
* offset by 100. * offset by 100.
*/ */
static void static void nak(int error, const char *msg)
nak(int error, const char *msg)
{ {
struct tftphdr *tp; struct tftphdr *tp;
int length; int length;
tp = (struct tftphdr *)ackbuf; tp = (struct tftphdr *)ackbuf;
tp->th_opcode = htons((u_short)ERROR); tp->th_opcode = htons((u_short) ERROR);
tp->th_code = htons((u_short)error); tp->th_code = htons((u_short) error);
if ( error >= 100 ) { if (error >= 100) {
/* This is a Unix errno+100 */ /* This is a Unix errno+100 */
if ( !msg ) if (!msg)
msg = strerror(error - 100); msg = strerror(error - 100);
error = EUNDEF; error = EUNDEF;
} else { } else {
if ( (unsigned)error >= ERR_CNT ) if ((unsigned)error >= ERR_CNT)
error = EUNDEF; error = EUNDEF;
if ( !msg ) if (!msg)
msg = errmsgs[error]; msg = errmsgs[error];
} }
tp->th_code = htons((u_short)error); tp->th_code = htons((u_short) error);
length = strlen(msg)+1; length = strlen(msg) + 1;
memcpy(tp->th_msg, msg, length); memcpy(tp->th_msg, msg, length);
length += 4; /* Add space for header */ length += 4; /* Add space for header */
@ -363,13 +357,12 @@ nak(int error, const char *msg)
perror("nak"); perror("nak");
} }
static void static void tpacket(const char *s, struct tftphdr *tp, int n)
tpacket(const char *s, struct tftphdr *tp, int n)
{ {
static const char *opcodes[] = static const char *opcodes[] =
{ "#0", "RRQ", "WRQ", "DATA", "ACK", "ERROR", "OACK" }; { "#0", "RRQ", "WRQ", "DATA", "ACK", "ERROR", "OACK" };
char *cp, *file; char *cp, *file;
u_short op = ntohs((u_short)tp->th_opcode); u_short op = ntohs((u_short) tp->th_opcode);
if (op < RRQ || op > ERROR) if (op < RRQ || op > ERROR)
printf("%s opcode=%x ", s, op); printf("%s opcode=%x ", s, op);
@ -380,7 +373,7 @@ tpacket(const char *s, struct tftphdr *tp, int n)
case RRQ: case RRQ:
case WRQ: case WRQ:
n -= 2; n -= 2;
file = cp = (char *) &(tp->th_stuff); file = cp = (char *)&(tp->th_stuff);
cp = strchr(cp, '\0'); cp = strchr(cp, '\0');
printf("<file=%s, mode=%s>\n", file, cp + 1); printf("<file=%s, mode=%s>\n", file, cp + 1);
break; break;
@ -402,35 +395,31 @@ tpacket(const char *s, struct tftphdr *tp, int n)
struct timeval tstart; struct timeval tstart;
struct timeval tstop; struct timeval tstop;
static void static void startclock(void)
startclock(void)
{ {
(void)gettimeofday(&tstart, NULL); (void)gettimeofday(&tstart, NULL);
} }
static void static void stopclock(void)
stopclock(void)
{ {
(void)gettimeofday(&tstop, NULL); (void)gettimeofday(&tstop, NULL);
} }
static void static void printstats(const char *direction, unsigned long amount)
printstats(const char *direction, unsigned long amount)
{ {
double delta; double delta;
delta = (tstop.tv_sec+(tstop.tv_usec/100000.0)) - delta = (tstop.tv_sec + (tstop.tv_usec / 100000.0)) -
(tstart.tv_sec+(tstart.tv_usec/100000.0)); (tstart.tv_sec + (tstart.tv_usec / 100000.0));
if (verbose) { if (verbose) {
printf("%s %lu bytes in %.1f seconds", direction, amount, delta); printf("%s %lu bytes in %.1f seconds", direction, amount, delta);
printf(" [%.0f bit/s]", (amount*8.)/delta); printf(" [%.0f bit/s]", (amount * 8.) / delta);
putchar('\n'); putchar('\n');
} }
} }
static void static void timer(int sig)
timer(int sig)
{ {
int save_errno = errno; int save_errno = errno;

View file

@ -22,7 +22,7 @@
* Set the signal handler and flags. Basically a user-friendly * Set the signal handler and flags. Basically a user-friendly
* wrapper around sigaction(). * wrapper around sigaction().
*/ */
void set_signal(int signum, void (*handler)(int), int flags) void set_signal(int signum, void (*handler) (int), int flags)
{ {
struct sigaction sa; struct sigaction sa;
@ -31,7 +31,7 @@ void set_signal(int signum, void (*handler)(int), int flags)
sigemptyset(&sa.sa_mask); sigemptyset(&sa.sa_mask);
sa.sa_flags = flags; sa.sa_flags = flags;
if ( sigaction(signum, &sa, NULL) ) { if (sigaction(signum, &sa, NULL)) {
syslog(LOG_ERR, "sigaction: %m"); syslog(LOG_ERR, "sigaction: %m");
exit(EX_OSERR); exit(EX_OSERR);
} }
@ -44,7 +44,7 @@ void *tfmalloc(size_t size)
{ {
void *p = malloc(size); void *p = malloc(size);
if ( !p ) { if (!p) {
syslog(LOG_ERR, "malloc: %m"); syslog(LOG_ERR, "malloc: %m");
exit(EX_OSERR); exit(EX_OSERR);
} }
@ -59,11 +59,10 @@ char *tfstrdup(const char *str)
{ {
char *p = strdup(str); char *p = strdup(str);
if ( !p ) { if (!p) {
syslog(LOG_ERR, "strdup: %m"); syslog(LOG_ERR, "strdup: %m");
exit(EX_OSERR); exit(EX_OSERR);
} }
return p; return p;
} }

View file

@ -90,10 +90,9 @@ static int address_is_local(const struct sockaddr_in *addr)
return rv; return rv;
} }
int int
myrecvfrom(int s, void *buf, int len, unsigned int flags, myrecvfrom(int s, void *buf, int len, unsigned int flags,
struct sockaddr *from, socklen_t *fromlen, struct sockaddr *from, socklen_t * fromlen,
struct sockaddr_in *myaddr) struct sockaddr_in *myaddr)
{ {
struct msghdr msg; struct msghdr msg;
@ -134,35 +133,37 @@ myrecvfrom(int s, void *buf, int len, unsigned int flags,
msg.msg_iov = &iov; msg.msg_iov = &iov;
msg.msg_iovlen = 1; msg.msg_iovlen = 1;
if ( (n = recvmsg(s, &msg, flags)) < 0 ) if ((n = recvmsg(s, &msg, flags)) < 0)
return n; /* Error */ return n; /* Error */
*fromlen = msg.msg_namelen; *fromlen = msg.msg_namelen;
if ( myaddr ) { if (myaddr) {
bzero(myaddr, sizeof(struct sockaddr_in)); bzero(myaddr, sizeof(struct sockaddr_in));
myaddr->sin_family = AF_INET; myaddr->sin_family = AF_INET;
if ( msg.msg_controllen < sizeof(struct cmsghdr) || if (msg.msg_controllen < sizeof(struct cmsghdr) ||
(msg.msg_flags & MSG_CTRUNC) ) (msg.msg_flags & MSG_CTRUNC))
return n; /* No information available */ return n; /* No information available */
for ( cmptr = CMSG_FIRSTHDR(&msg) ; cmptr != NULL ; for (cmptr = CMSG_FIRSTHDR(&msg); cmptr != NULL;
cmptr = CMSG_NXTHDR(&msg, cmptr) ) { cmptr = CMSG_NXTHDR(&msg, cmptr)) {
#ifdef IP_RECVDSTADDR #ifdef IP_RECVDSTADDR
if ( cmptr->cmsg_level == IPPROTO_IP && if (cmptr->cmsg_level == IPPROTO_IP &&
cmptr->cmsg_type == IP_RECVDSTADDR ) { cmptr->cmsg_type == IP_RECVDSTADDR) {
memcpy(&myaddr->sin_addr, CMSG_DATA(cmptr), memcpy(&myaddr->sin_addr, CMSG_DATA(cmptr),
sizeof(struct in_addr)); sizeof(struct in_addr));
} }
#endif #endif
#ifdef IP_PKTINFO #ifdef IP_PKTINFO
if ( cmptr->cmsg_level == IPPROTO_IP && if (cmptr->cmsg_level == IPPROTO_IP &&
cmptr->cmsg_type == IP_PKTINFO ) { cmptr->cmsg_type == IP_PKTINFO) {
memcpy(&pktinfo, CMSG_DATA(cmptr), sizeof(struct in_pktinfo)); memcpy(&pktinfo, CMSG_DATA(cmptr),
memcpy(&myaddr->sin_addr, &pktinfo.ipi_addr, sizeof(struct in_addr)); sizeof(struct in_pktinfo));
memcpy(&myaddr->sin_addr, &pktinfo.ipi_addr,
sizeof(struct in_addr));
} }
#endif #endif
@ -180,8 +181,7 @@ myrecvfrom(int s, void *buf, int len, unsigned int flags,
int int
myrecvfrom(int s, void *buf, int len, unsigned int flags, myrecvfrom(int s, void *buf, int len, unsigned int flags,
struct sockaddr *from, int *fromlen, struct sockaddr *from, int *fromlen, struct sockaddr_in *myaddr)
struct sockaddr_in *myaddr)
{ {
/* There is no way we can get the local address, fudge it */ /* There is no way we can get the local address, fudge it */
@ -191,7 +191,7 @@ myrecvfrom(int s, void *buf, int len, unsigned int flags,
myaddr->sin_port = htons(IPPORT_TFTP); myaddr->sin_port = htons(IPPORT_TFTP);
bzero(&myaddr->sin_addr, sizeof(myaddr->sin_addr)); bzero(&myaddr->sin_addr, sizeof(myaddr->sin_addr));
return recvfrom(s,buf,len,flags,from,fromlen); return recvfrom(s, buf, len, flags, from, fromlen);
} }
#endif #endif

View file

@ -19,5 +19,5 @@
int int
myrecvfrom(int s, void *buf, int len, unsigned int flags, myrecvfrom(int s, void *buf, int len, unsigned int flags,
struct sockaddr *from, socklen_t *fromlen, struct sockaddr *from, socklen_t * fromlen,
struct sockaddr_in *myaddr); struct sockaddr_in *myaddr);

View file

@ -58,10 +58,11 @@ static int xform_tolower(int c)
} }
/* Do \-substitution. Call with string == NULL to get length only. */ /* Do \-substitution. Call with string == NULL to get length only. */
static int genmatchstring(char *string, const char *pattern, const char *input, static int genmatchstring(char *string, const char *pattern,
const regmatch_t *pmatch, match_pattern_callback macrosub) const char *input, const regmatch_t * pmatch,
match_pattern_callback macrosub)
{ {
int (*xform)(int) = xform_null; int (*xform) (int) = xform_null;
int len = 0; int len = 0;
int n, mlen, sublen; int n, mlen, sublen;
int endbytes; int endbytes;
@ -69,28 +70,36 @@ static int genmatchstring(char *string, const char *pattern, const char *input,
/* Get section before match; note pmatch[0] is the whole match */ /* Get section before match; note pmatch[0] is the whole match */
endbytes = strlen(input) - pmatch[0].rm_eo; endbytes = strlen(input) - pmatch[0].rm_eo;
len = pmatch[0].rm_so + endbytes; len = pmatch[0].rm_so + endbytes;
if ( string ) { if (string) {
memcpy(string, input, pmatch[0].rm_so); memcpy(string, input, pmatch[0].rm_so);
string += pmatch[0].rm_so; string += pmatch[0].rm_so;
} }
/* Transform matched section */ /* Transform matched section */
while ( *pattern ) { while (*pattern) {
mlen = 0; mlen = 0;
if ( *pattern == '\\' && pattern[1] != '\0' ) { if (*pattern == '\\' && pattern[1] != '\0') {
char macro = pattern[1]; char macro = pattern[1];
switch ( macro ) { switch (macro) {
case '0': case '1': case '2': case '3': case '4': case '0':
case '5': case '6': case '7': case '8': case '9': case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
n = pattern[1] - '0'; n = pattern[1] - '0';
if ( pmatch[n].rm_so != -1 ) { if (pmatch[n].rm_so != -1) {
mlen = pmatch[n].rm_eo - pmatch[n].rm_so; mlen = pmatch[n].rm_eo - pmatch[n].rm_so;
len += mlen; len += mlen;
if ( string ) { if (string) {
const char *p = input+pmatch[n].rm_so; const char *p = input + pmatch[n].rm_so;
while ( mlen-- ) while (mlen--)
*string++ = xform(*p++); *string++ = xform(*p++);
} }
} }
@ -109,33 +118,32 @@ static int genmatchstring(char *string, const char *pattern, const char *input,
break; break;
default: default:
if ( macrosub && if (macrosub && (sublen = macrosub(macro, string)) >= 0) {
(sublen = macrosub(macro, string)) >= 0 ) { while (sublen--) {
while ( sublen-- ) {
len++; len++;
if ( string ) { if (string) {
*string = xform(*string); *string = xform(*string);
string++; string++;
} }
} }
} else { } else {
len++; len++;
if ( string ) if (string)
*string++ = xform(pattern[1]); *string++ = xform(pattern[1]);
} }
} }
pattern += 2; pattern += 2;
} else { } else {
len++; len++;
if ( string ) if (string)
*string++ = xform(*pattern); *string++ = xform(*pattern);
pattern++; pattern++;
} }
} }
/* Copy section after match */ /* Copy section after match */
if ( string ) { if (string) {
memcpy(string, input+pmatch[0].rm_eo, endbytes); memcpy(string, input + pmatch[0].rm_eo, endbytes);
string[endbytes] = '\0'; string[endbytes] = '\0';
} }
@ -152,23 +160,23 @@ static int readescstring(char *buf, char **str)
char *p = *str; char *p = *str;
int wasbs = 0, len = 0; int wasbs = 0, len = 0;
while ( *p && isspace(*p) ) while (*p && isspace(*p))
p++; p++;
if ( ! *p ) { if (!*p) {
*buf = '\0'; *buf = '\0';
*str = p; *str = p;
return 0; return 0;
} }
while ( *p ) { while (*p) {
if ( !wasbs && (isspace(*p) || *p == '#') ) { if (!wasbs && (isspace(*p) || *p == '#')) {
*buf = '\0'; *buf = '\0';
*str = p; *str = p;
return len; return len;
} }
/* Important: two backslashes leave us in the !wasbs state! */ /* Important: two backslashes leave us in the !wasbs state! */
wasbs = !wasbs && ( *p == '\\' ); wasbs = !wasbs && (*p == '\\');
*buf++ = *p++; *buf++ = *p++;
len++; len++;
} }
@ -190,11 +198,11 @@ static int parseline(char *line, struct rule *r, int lineno)
memset(r, 0, sizeof *r); memset(r, 0, sizeof *r);
r->nrule = nrule; r->nrule = nrule;
if ( !readescstring(buffer, &line) ) if (!readescstring(buffer, &line))
return 0; /* No rule found */ return 0; /* No rule found */
for ( p = buffer ; *p ; p++ ) { for (p = buffer; *p; p++) {
switch(*p) { switch (*p) {
case 'r': case 'r':
r->rule_flags |= RULE_REWRITE; r->rule_flags |= RULE_REWRITE;
break; break;
@ -223,7 +231,8 @@ static int parseline(char *line, struct rule *r, int lineno)
r->rule_flags |= RULE_INVERSE; r->rule_flags |= RULE_INVERSE;
break; break;
default: default:
syslog(LOG_ERR, "Remap command \"%s\" on line %d contains invalid char \"%c\"", syslog(LOG_ERR,
"Remap command \"%s\" on line %d contains invalid char \"%c\"",
buffer, lineno, *p); buffer, lineno, *p);
return -1; /* Error */ return -1; /* Error */
break; break;
@ -231,30 +240,32 @@ static int parseline(char *line, struct rule *r, int lineno)
} }
/* RULE_GLOBAL only applies when RULE_REWRITE specified */ /* RULE_GLOBAL only applies when RULE_REWRITE specified */
if ( !(r->rule_flags & RULE_REWRITE) ) if (!(r->rule_flags & RULE_REWRITE))
r->rule_flags &= ~RULE_GLOBAL; r->rule_flags &= ~RULE_GLOBAL;
if ( (r->rule_flags & (RULE_INVERSE|RULE_REWRITE)) == if ((r->rule_flags & (RULE_INVERSE | RULE_REWRITE)) ==
(RULE_INVERSE|RULE_REWRITE) ) { (RULE_INVERSE | RULE_REWRITE)) {
syslog(LOG_ERR, "r rules cannot be inverted, line %d: %s\n", lineno, line); syslog(LOG_ERR, "r rules cannot be inverted, line %d: %s\n",
lineno, line);
return -1; /* Error */ return -1; /* Error */
} }
/* Read and compile the regex */ /* Read and compile the regex */
if ( !readescstring(buffer, &line) ) { if (!readescstring(buffer, &line)) {
syslog(LOG_ERR, "No regex on remap line %d: %s\n", lineno, line); syslog(LOG_ERR, "No regex on remap line %d: %s\n", lineno, line);
return -1; /* Error */ return -1; /* Error */
} }
if ( (rv = regcomp(&r->rx, buffer, rxflags)) != 0 ) { if ((rv = regcomp(&r->rx, buffer, rxflags)) != 0) {
char errbuf[BUFSIZ]; char errbuf[BUFSIZ];
regerror(rv, &r->rx, errbuf, BUFSIZ); regerror(rv, &r->rx, errbuf, BUFSIZ);
syslog(LOG_ERR, "Bad regex in remap line %d: %s\n", lineno, errbuf); syslog(LOG_ERR, "Bad regex in remap line %d: %s\n", lineno,
errbuf);
return -1; /* Error */ return -1; /* Error */
} }
/* Read the rewrite pattern, if any */ /* Read the rewrite pattern, if any */
if ( readescstring(buffer, &line) ) { if (readescstring(buffer, &line)) {
r->pattern = tfstrdup(buffer); r->pattern = tfstrdup(buffer);
} else { } else {
r->pattern = ""; r->pattern = "";
@ -265,7 +276,7 @@ static int parseline(char *line, struct rule *r, int lineno)
} }
/* Read a rule file */ /* Read a rule file */
struct rule *parserulefile(FILE *f) struct rule *parserulefile(FILE * f)
{ {
char line[MAXLINE]; char line[MAXLINE];
struct rule *first_rule = NULL; struct rule *first_rule = NULL;
@ -275,11 +286,11 @@ struct rule *parserulefile(FILE *f)
int lineno = 0; int lineno = 0;
int err = 0; int err = 0;
while ( lineno++, fgets(line, MAXLINE, f) ) { while (lineno++, fgets(line, MAXLINE, f)) {
rv = parseline(line, this_rule, lineno); rv = parseline(line, this_rule, lineno);
if ( rv < 0 ) if (rv < 0)
err = 1; err = 1;
if ( rv > 0 ) { if (rv > 0) {
*last_rule = this_rule; *last_rule = this_rule;
last_rule = &this_rule->next; last_rule = &this_rule->next;
this_rule = tfmalloc(sizeof(struct rule)); this_rule = tfmalloc(sizeof(struct rule));
@ -288,7 +299,7 @@ struct rule *parserulefile(FILE *f)
free(this_rule); /* Last one is always unused */ free(this_rule); /* Last one is always unused */
if ( err ) { if (err) {
/* Bail on error, we have already logged an error message */ /* Bail on error, we have already logged an error message */
exit(EX_CONFIG); exit(EX_CONFIG);
} }
@ -301,13 +312,13 @@ void freerules(struct rule *r)
{ {
struct rule *next; struct rule *next;
while ( r ) { while (r) {
next = r->next; next = r->next;
regfree(&r->rx); regfree(&r->rx);
/* "" patterns aren't allocated by malloc() */ /* "" patterns aren't allocated by malloc() */
if ( r->pattern && *r->pattern ) if (r->pattern && *r->pattern)
free((void *)r->pattern); free((void *)r->pattern);
free(r); free(r);
@ -332,46 +343,48 @@ char *rewrite_string(const char *input, const struct rule *rules,
/* Default error */ /* Default error */
*errmsg = "Remap table failure"; *errmsg = "Remap table failure";
if ( verbosity >= 3 ) { if (verbosity >= 3) {
syslog(LOG_INFO, "remap: input: %s", current); syslog(LOG_INFO, "remap: input: %s", current);
} }
for ( ruleptr = rules ; ruleptr ; ruleptr = ruleptr->next ) { for (ruleptr = rules; ruleptr; ruleptr = ruleptr->next) {
if ( ((ruleptr->rule_flags & RULE_GETONLY) && is_put) || if (((ruleptr->rule_flags & RULE_GETONLY) && is_put) ||
((ruleptr->rule_flags & RULE_PUTONLY) && !is_put) ) { ((ruleptr->rule_flags & RULE_PUTONLY) && !is_put)) {
continue; /* Rule not applicable, try next */ continue; /* Rule not applicable, try next */
} }
if ( ! deadman-- ) { if (!deadman--) {
syslog(LOG_WARNING, "remap: Breaking loop, input = %s, last = %s", syslog(LOG_WARNING,
input, current); "remap: Breaking loop, input = %s, last = %s", input,
current);
free(current); free(current);
return NULL; /* Did not terminate! */ return NULL; /* Did not terminate! */
} }
do { do {
if ( regexec(&ruleptr->rx, current, 10, pmatch, 0) == if (regexec(&ruleptr->rx, current, 10, pmatch, 0) ==
(ruleptr->rule_flags & RULE_INVERSE ? REG_NOMATCH : 0) ) { (ruleptr->rule_flags & RULE_INVERSE ? REG_NOMATCH : 0)) {
/* Match on this rule */ /* Match on this rule */
was_match = 1; was_match = 1;
if ( ruleptr->rule_flags & RULE_INVERSE ) { if (ruleptr->rule_flags & RULE_INVERSE) {
/* No actual match, so clear out the pmatch array */ /* No actual match, so clear out the pmatch array */
int i; int i;
for ( i = 0 ; i < 10 ; i++ ) for (i = 0; i < 10; i++)
pmatch[i].rm_so = pmatch[i].rm_eo = -1; pmatch[i].rm_so = pmatch[i].rm_eo = -1;
} }
if ( ruleptr->rule_flags & RULE_ABORT ) { if (ruleptr->rule_flags & RULE_ABORT) {
if ( verbosity >= 3 ) { if (verbosity >= 3) {
syslog(LOG_INFO, "remap: rule %d: abort: %s", syslog(LOG_INFO, "remap: rule %d: abort: %s",
ruleptr->nrule, current); ruleptr->nrule, current);
} }
if ( ruleptr->pattern[0] ) { if (ruleptr->pattern[0]) {
/* Custom error message */ /* Custom error message */
len = genmatchstring(NULL, ruleptr->pattern, current, len =
genmatchstring(NULL, ruleptr->pattern, current,
pmatch, macrosub); pmatch, macrosub);
newstr = tfmalloc(len+1); newstr = tfmalloc(len + 1);
genmatchstring(newstr, ruleptr->pattern, current, genmatchstring(newstr, ruleptr->pattern, current,
pmatch, macrosub); pmatch, macrosub);
*errmsg = newstr; *errmsg = newstr;
@ -379,18 +392,18 @@ char *rewrite_string(const char *input, const struct rule *rules,
*errmsg = NULL; *errmsg = NULL;
} }
free(current); free(current);
return(NULL); return (NULL);
} }
if ( ruleptr->rule_flags & RULE_REWRITE ) { if (ruleptr->rule_flags & RULE_REWRITE) {
len = genmatchstring(NULL, ruleptr->pattern, current, len = genmatchstring(NULL, ruleptr->pattern, current,
pmatch, macrosub); pmatch, macrosub);
newstr = tfmalloc(len+1); newstr = tfmalloc(len + 1);
genmatchstring(newstr, ruleptr->pattern, current, genmatchstring(newstr, ruleptr->pattern, current,
pmatch, macrosub); pmatch, macrosub);
free(current); free(current);
current = newstr; current = newstr;
if ( verbosity >= 3 ) { if (verbosity >= 3) {
syslog(LOG_INFO, "remap: rule %d: rewrite: %s", syslog(LOG_INFO, "remap: rule %d: rewrite: %s",
ruleptr->nrule, current); ruleptr->nrule, current);
} }
@ -399,26 +412,28 @@ char *rewrite_string(const char *input, const struct rule *rules,
break; /* No match, terminate unconditionally */ break; /* No match, terminate unconditionally */
} }
/* If the rule is global, keep going until no match */ /* If the rule is global, keep going until no match */
} while ( ruleptr->rule_flags & RULE_GLOBAL ); } while (ruleptr->rule_flags & RULE_GLOBAL);
if ( was_match ) { if (was_match) {
was_match = 0; was_match = 0;
if ( ruleptr->rule_flags & RULE_EXIT ) { if (ruleptr->rule_flags & RULE_EXIT) {
if ( verbosity >= 3 ) { if (verbosity >= 3) {
syslog(LOG_INFO, "remap: rule %d: exit", ruleptr->nrule); syslog(LOG_INFO, "remap: rule %d: exit",
ruleptr->nrule);
} }
return current; /* Exit here, we're done */ return current; /* Exit here, we're done */
} else if ( ruleptr->rule_flags & RULE_RESTART ) { } else if (ruleptr->rule_flags & RULE_RESTART) {
ruleptr = rules; /* Start from the top */ ruleptr = rules; /* Start from the top */
if ( verbosity >= 3 ) { if (verbosity >= 3) {
syslog(LOG_INFO, "remap: rule %d: restart", ruleptr->nrule); syslog(LOG_INFO, "remap: rule %d: restart",
ruleptr->nrule);
} }
} }
} }
} }
if ( verbosity >= 3 ) { if (verbosity >= 3) {
syslog(LOG_INFO, "remap: done"); syslog(LOG_INFO, "remap: done");
} }
return current; return current;

View file

@ -26,7 +26,7 @@ struct rule;
macro character is passed as the first argument; the output buffer, macro character is passed as the first argument; the output buffer,
if any, is passed as the second argument. The function should return if any, is passed as the second argument. The function should return
the number of characters output, or -1 on failure. */ the number of characters output, or -1 on failure. */
typedef int (*match_pattern_callback)(char, char *); typedef int (*match_pattern_callback) (char, char *);
/* Read a rule file */ /* Read a rule file */
struct rule *parserulefile(FILE *); struct rule *parserulefile(FILE *);
@ -40,4 +40,3 @@ char *rewrite_string(const char *, const struct rule *, int,
#endif /* WITH_REGEX */ #endif /* WITH_REGEX */
#endif /* TFTPD_REMAP_H */ #endif /* TFTPD_REMAP_H */

File diff suppressed because it is too large Load diff