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
/* 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 const char *rcsid UNUSED =
"tftp-hpa: $Id$";
static const char *rcsid UNUSED = "tftp-hpa: $Id$";
#endif /* not lint */
/* 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);
struct tftphdr *w_init() { return rw_init(0); } /* write-behind */
struct tftphdr *r_init() { return rw_init(1); } /* read-ahead */
struct tftphdr *w_init()
{
return rw_init(0);
} /* write-behind */
struct tftphdr *r_init()
{
return rw_init(1);
} /* read-ahead */
/* init for either read-ahead or write-behind */
/* x == zero for write-behind, one for read-head */
static struct tftphdr *
rw_init(int x)
static struct tftphdr *rw_init(int x)
{
newline = 0; /* init crlf flag */
prevchar = -1;
@ -98,12 +103,10 @@ rw_init(int x)
return (struct tftphdr *)bfs[0].buf;
}
/* Have emptied current buffer by sending to net and getting ack.
Free it and return next buffer filled with data.
*/
int
readit(FILE *file, struct tftphdr **dpp, int convert)
int readit(FILE * file, struct tftphdr **dpp, int convert)
{
struct bf *b;
@ -113,7 +116,7 @@ readit(FILE *file, struct tftphdr **dpp, int convert)
b = &bfs[current]; /* look at new buffer */
if (b->counter == BF_FREE) /* if it's empty */
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 */
return b->counter;
}
@ -122,8 +125,7 @@ readit(FILE *file, struct tftphdr **dpp, int convert)
* fill the input buffer, doing ascii conversions if requested
* conversions are lf -> cr,lf and cr -> cr, nul
*/
void
read_ahead(FILE *file, int convert)
void read_ahead(FILE * file, int convert)
{
int i;
char *p;
@ -144,16 +146,17 @@ read_ahead(FILE *file, int convert)
}
p = dp->th_data;
for (i = 0 ; i < segsize; i++) {
for (i = 0; i < segsize; i++) {
if (newline) {
if (prevchar == '\n')
c = '\n'; /* lf to cr,lf */
else c = '\0'; /* cr to cr,nul */
else
c = '\0'; /* cr to cr,nul */
newline = 0;
}
else {
} else {
c = getc(file);
if (c == EOF) break;
if (c == EOF)
break;
if (c == '\n' || c == '\r') {
prevchar = c;
c = '\r';
@ -169,8 +172,7 @@ read_ahead(FILE *file, int convert)
from the queue. Calls write_behind only if next buffer not
available.
*/
int
writeit(FILE *file, struct tftphdr **dpp, int ct, int convert)
int writeit(FILE * file, struct tftphdr **dpp, int ct, int convert)
{
bfs[current].counter = ct; /* set size of data to write */
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
* CR followed by anything else. In this case we leave it alone.
*/
int
write_behind(FILE *file, int convert)
int write_behind(FILE * file, int convert)
{
char *buf;
int count;
@ -208,7 +209,8 @@ write_behind(FILE *file, int convert)
nextone = !nextone; /* incr for next time */
buf = dp->th_data;
if (count <= 0) return -1; /* nak logic? */
if (count <= 0)
return -1; /* nak logic? */
if (convert == 0)
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 (c == '\n') /* if have cr,lf then just */
fseek(file, -1, 1); /* smash lf on top of the cr */
else
if (c == '\0') /* if have cr,nul then */
else if (c == '\0') /* if have cr,nul then */
goto skipit; /* just skip over the putc */
/* else just fall through and allow it */
}
putc(c, file);
skipit:
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.
@ -244,9 +244,8 @@ skipit:
* when trace is active).
*/
int
synchnet(int f) /* socket to flush */
{
int synchnet(int f)
{ /* socket to flush */
int pktcount = 0;
char rbuf[PKTSIZE];
struct sockaddr_in from;
@ -254,27 +253,28 @@ synchnet(int f) /* socket to flush */
fd_set socketset;
struct timeval notime;
while ( 1 ) {
while (1) {
notime.tv_sec = notime.tv_usec = 0;
FD_ZERO(&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 */
/* Otherwise drain the packet */
pktcount++;
fromlen = sizeof from;
(void) recvfrom(f, rbuf, sizeof (rbuf), 0,
(void)recvfrom(f, rbuf, sizeof(rbuf), 0,
(struct sockaddr *)&from, &fromlen);
}
return pktcount; /* Return packets drained */
}
int pick_port_bind(int sockfd, struct sockaddr_in *myaddr, unsigned int port_range_from, unsigned int port_range_to)
int pick_port_bind(int sockfd, struct sockaddr_in *myaddr,
unsigned int port_range_from,
unsigned int port_range_to)
{
unsigned int port, firstport;
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
? port_range_from + rand() % (port_range_to-port_range_from+1)
? port_range_from + rand() % (port_range_to - port_range_from + 1)
: 0;
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) {
/* 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;
/* 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++;
if ( port > port_range_to )
if (port > port_range_to)
port = port_range_from;
} while ( port != firstport );
} while (port != firstport);
return -1;
}

View file

@ -62,6 +62,7 @@ int writeit(FILE *, struct tftphdr **, int, int);
extern int segsize;
#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

View file

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

View file

@ -6,8 +6,7 @@
#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;
memset(&action, 0, sizeof action);

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -19,5 +19,5 @@
int
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);

View file

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

View file

@ -26,7 +26,7 @@ struct rule;
macro character is passed as the first argument; the output buffer,
if any, is passed as the second argument. The function should return
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 */
struct rule *parserulefile(FILE *);
@ -40,4 +40,3 @@ char *rewrite_string(const char *, const struct rule *, int,
#endif /* WITH_REGEX */
#endif /* TFTPD_REMAP_H */

File diff suppressed because it is too large Load diff