summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormglocker <mglocker@openbsd.org>2006-07-12 16:58:51 +0000
committermglocker <mglocker@openbsd.org>2006-07-12 16:58:51 +0000
commite9938b02689dcb2c3dbecbf65b8fe074eea1b2d3 (patch)
tree99de4e31b5ce167aeb353c24920d3ec9bd1e6368
parentinclude uvm_extern.h for atop() (diff)
downloadwireguard-openbsd-e9938b02689dcb2c3dbecbf65b8fe074eea1b2d3.tar.xz
wireguard-openbsd-e9938b02689dcb2c3dbecbf65b8fe074eea1b2d3.zip
knf. no binary change.
ok beck@ claudio@
-rw-r--r--usr.bin/tftp/main.c247
-rw-r--r--usr.bin/tftp/tftp.c95
-rw-r--r--usr.bin/tftp/tftpsubs.c228
-rw-r--r--usr.bin/tftp/tftpsubs.h16
4 files changed, 300 insertions, 286 deletions
diff --git a/usr.bin/tftp/main.c b/usr.bin/tftp/main.c
index a41b14e8e7c..4249eb2cc53 100644
--- a/usr.bin/tftp/main.c
+++ b/usr.bin/tftp/main.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: main.c,v 1.24 2006/05/08 13:02:51 claudio Exp $ */
+/* $OpenBSD: main.c,v 1.25 2006/07/12 16:58:51 mglocker Exp $ */
/* $NetBSD: main.c,v 1.6 1995/05/21 16:54:10 mycroft Exp $ */
/*
@@ -40,13 +40,14 @@ static const char copyright[] =
#if 0
static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93";
#endif
-static const char rcsid[] = "$OpenBSD: main.c,v 1.24 2006/05/08 13:02:51 claudio Exp $";
+static const char rcsid[] =
+ "$OpenBSD: main.c,v 1.25 2006/07/12 16:58:51 mglocker Exp $";
#endif /* not lint */
-/* Many bug fixes are from Jim Guyton <guyton@rand-unix> */
-
/*
- * TFTP User Program -- Command Interface.
+ * TFTP User Program -- Command Interface
+ *
+ * This version includes many modifications by Jim Guyton <guyton@rand-unix>
*/
#include <sys/param.h>
@@ -57,15 +58,15 @@ static const char rcsid[] = "$OpenBSD: main.c,v 1.24 2006/05/08 13:02:51 claudio
#include <arpa/inet.h>
#include <ctype.h>
+#include <err.h>
#include <errno.h>
#include <netdb.h>
+#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
-#include <err.h>
-#include <poll.h>
#include "extern.h"
@@ -74,53 +75,46 @@ static const char rcsid[] = "$OpenBSD: main.c,v 1.24 2006/05/08 13:02:51 claudio
#define MAXARGV 20
#define HELPINDENT (sizeof("connect"))
-struct sockaddr_in peeraddr;
-int f;
-short port;
-int trace;
-int verbose;
-int connected;
-char mode[32];
-char line[LBUFLEN];
-int margc;
-char *margv[MAXARGV+1];
-char *prompt = "tftp";
-void intr(int);
-struct servent *sp;
-int rexmtval = TIMEOUT;
-int maxtimeout = 5 * TIMEOUT;
-char hostname[MAXHOSTNAMELEN];
-FILE *file = NULL;
-
-volatile sig_atomic_t intrflag = 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 **);
-int readcmd(char *, int, FILE *);
-
-static __dead void command(void);
-
-static void getusage(char *);
-static int makeargv(void);
-static void putusage(char *);
-static void settftpmode(char *);
-
-struct cmd {
- char *name;
- char *help;
- void (*handler)(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 **);
+int readcmd(char *, int, FILE *);
+static void getusage(char *);
+static int makeargv(void);
+static void putusage(char *);
+static void settftpmode(char *);
+static __dead void command(void);
+struct cmd *getcmd(char *);
+char *tail(char *);
+
+struct sockaddr_in peeraddr;
+int f;
+short port;
+int trace;
+int verbose;
+int connected;
+char mode[32];
+char line[LBUFLEN];
+int margc;
+char *margv[MAXARGV+1];
+char *prompt = "tftp";
+void intr(int);
+struct servent *sp;
+int rexmtval = TIMEOUT;
+int maxtimeout = 5 * TIMEOUT;
+char hostname[MAXHOSTNAMELEN];
+FILE *file = NULL;
+volatile sig_atomic_t intrflag = 0;
char vhelp[] = "toggle verbose mode";
char thelp[] = "toggle packet tracing";
@@ -136,27 +130,33 @@ char ihelp[] = "set total retransmission timeout";
char ashelp[] = "set mode to netascii";
char bnhelp[] = "set mode to octet";
+struct cmd {
+ char *name;
+ char *help;
+ void (*handler)(int, char **);
+};
+
struct cmd cmdtab[] = {
- { "connect", chelp, setpeer },
- { "mode", mhelp, modecmd },
- { "put", shelp, put },
- { "get", rhelp, get },
- { "quit", qhelp, quit },
- { "verbose", vhelp, setverbose },
- { "trace", thelp, settrace },
- { "status", sthelp, status },
- { "binary", bnhelp, setbinary },
- { "ascii", ashelp, setascii },
- { "rexmt", xhelp, setrexmt },
- { "timeout", ihelp, settimeout },
- { "help", hhelp, help },
- { "?", hhelp, help },
- { NULL, NULL, NULL }
+ { "connect", chelp, setpeer },
+ { "mode", mhelp, modecmd },
+ { "put", shelp, put },
+ { "get", rhelp, get },
+ { "quit", qhelp, quit },
+ { "verbose", vhelp, setverbose },
+ { "trace", thelp, settrace },
+ { "status", sthelp, status },
+ { "binary", bnhelp, setbinary },
+ { "ascii", ashelp, setascii },
+ { "rexmt", xhelp, setrexmt },
+ { "timeout", ihelp, settimeout },
+ { "help", hhelp, help },
+ { "?", hhelp, help },
+ { NULL, NULL, NULL }
};
struct modes {
- char *m_name;
- char *m_mode;
+ char *m_name;
+ char *m_mode;
} modes[] = {
{ "ascii", "netascii" },
{ "netascii", "netascii" },
@@ -167,13 +167,10 @@ struct modes {
{ NULL, NULL }
};
-struct cmd *getcmd(char *);
-char *tail(char *);
-
int
main(int argc, char *argv[])
{
- struct sockaddr_in s_in;
+ struct sockaddr_in s_in;
/* socket, bind */
sp = getservbyname("tftp", "udp");
@@ -206,12 +203,12 @@ main(int argc, char *argv[])
void
setpeer(int argc, char *argv[])
{
- struct hostent *host;
+ struct hostent *host;
if (argc < 2) {
- strlcpy(line, "Connect ", sizeof line);
+ strlcpy(line, "Connect ", sizeof(line));
printf("(to) ");
- readcmd(&line[strlen(line)], LBUFLEN-strlen(line), stdin);
+ readcmd(&line[strlen(line)], LBUFLEN - strlen(line), stdin);
if (makeargv())
return;
argc = margc;
@@ -223,8 +220,8 @@ setpeer(int argc, char *argv[])
}
if (inet_aton(argv[1], &peeraddr.sin_addr) != 0) {
peeraddr.sin_family = AF_INET;
- (void) strncpy(hostname, argv[1], sizeof hostname);
- hostname[sizeof(hostname)-1] = '\0';
+ (void)strncpy(hostname, argv[1], sizeof(hostname));
+ hostname[sizeof(hostname) - 1] = '\0';
} else {
host = gethostbyname(argv[1]);
if (host == 0) {
@@ -234,7 +231,7 @@ setpeer(int argc, char *argv[])
}
peeraddr.sin_family = host->h_addrtype;
bcopy(host->h_addr, &peeraddr.sin_addr, host->h_length);
- (void) strlcpy(hostname, host->h_name, sizeof hostname);
+ (void)strlcpy(hostname, host->h_name, sizeof(hostname));
}
port = sp->s_port;
if (argc == 3) {
@@ -252,8 +249,8 @@ setpeer(int argc, char *argv[])
void
modecmd(int argc, char *argv[])
{
- struct modes *p;
- char *sep;
+ struct modes *p;
+ char *sep;
if (argc < 2) {
printf("Using %s mode to transfer files.\n", mode);
@@ -279,6 +276,7 @@ modecmd(int argc, char *argv[])
sep = " | ";
}
printf(" ]\n");
+
return;
}
@@ -297,7 +295,7 @@ setascii(int argc, char *argv[])
static void
settftpmode(char *newmode)
{
- strlcpy(mode, newmode, sizeof mode);
+ strlcpy(mode, newmode, sizeof(mode));
if (verbose)
printf("mode set to %s\n", mode);
}
@@ -308,12 +306,12 @@ settftpmode(char *newmode)
void
put(int argc, char *argv[])
{
- int fd;
- int n;
- char *cp, *targ;
+ int fd;
+ int n;
+ char *cp, *targ;
if (argc < 2) {
- strlcpy(line, "send ", sizeof line);
+ strlcpy(line, "send ", sizeof(line));
printf("(file) ");
readcmd(&line[strlen(line)], LBUFLEN - strlen(line), stdin);
if (makeargv())
@@ -327,8 +325,8 @@ put(int argc, char *argv[])
}
targ = argv[argc - 1];
if (strchr(argv[argc - 1], ':')) {
- char *cp;
- struct hostent *hp;
+ char *cp;
+ struct hostent *hp;
for (n = 1; n < argc - 1; n++)
if (strchr(argv[n], ':')) {
@@ -347,7 +345,7 @@ put(int argc, char *argv[])
peeraddr.sin_family = hp->h_addrtype;
connected = 1;
port = sp->s_port;
- strlcpy(hostname, hp->h_name, sizeof hostname);
+ strlcpy(hostname, hp->h_name, sizeof(hostname));
}
if (!connected) {
printf("No target machine specified.\n");
@@ -404,15 +402,15 @@ putusage(char *s)
void
get(int argc, char *argv[])
{
- int fd;
- int n;
- char *cp;
- char *src;
+ int fd;
+ int n;
+ char *cp;
+ char *src;
if (argc < 2) {
- strlcpy(line, "get ", sizeof line);
+ strlcpy(line, "get ", sizeof(line));
printf("(files) ");
- readcmd(&line[strlen(line)], LBUFLEN-strlen(line), stdin);
+ readcmd(&line[strlen(line)], LBUFLEN - strlen(line), stdin);
if (makeargv())
return;
argc = margc;
@@ -423,18 +421,18 @@ get(int argc, char *argv[])
return;
}
if (!connected) {
- for (n = 1; n < argc ; n++)
+ for (n = 1; n < argc; n++)
if (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 (src == NULL)
src = argv[n];
else {
- struct hostent *hp;
+ struct hostent *hp;
*src++ = 0;
hp = gethostbyname(argv[n]);
@@ -446,7 +444,7 @@ get(int argc, char *argv[])
hp->h_length);
peeraddr.sin_family = hp->h_addrtype;
connected = 1;
- strlcpy(hostname, hp->h_name, sizeof hostname);
+ strlcpy(hostname, hp->h_name, sizeof(hostname));
}
if (argc < 4) {
cp = argc == 3 ? argv[2] : tail(src);
@@ -462,7 +460,7 @@ get(int argc, char *argv[])
recvfile(fd, src, mode);
break;
}
- cp = tail(src); /* new .. jdg */
+ cp = tail(src); /* new .. jdg */
fd = creat(cp, 0644);
if (fd < 0) {
warn("create: %s", cp);
@@ -486,12 +484,12 @@ getusage(char *s)
void
setrexmt(int argc, char *argv[])
{
- int t;
+ int t;
if (argc < 2) {
- strlcpy(line, "Rexmt-timeout ", sizeof line);
+ strlcpy(line, "Rexmt-timeout ", sizeof(line));
printf("(value) ");
- readcmd(&line[strlen(line)], LBUFLEN-strlen(line), stdin);
+ readcmd(&line[strlen(line)], LBUFLEN - strlen(line), stdin);
if (makeargv())
return;
argc = margc;
@@ -511,12 +509,12 @@ setrexmt(int argc, char *argv[])
void
settimeout(int argc, char *argv[])
{
- int t;
+ int t;
if (argc < 2) {
- strlcpy(line, "Maximum-timeout ", sizeof line);
+ strlcpy(line, "Maximum-timeout ", sizeof(line));
printf("(value) ");
- readcmd(&line[strlen(line)], LBUFLEN-strlen(line), stdin);
+ readcmd(&line[strlen(line)], LBUFLEN - strlen(line), stdin);
if (makeargv())
return;
argc = margc;
@@ -540,10 +538,10 @@ status(int argc, char *argv[])
printf("Connected to %s.\n", hostname);
else
printf("Not connected.\n");
- printf("Mode: %s Verbose: %s Tracing: %s\n", mode,
- verbose ? "on" : "off", trace ? "on" : "off");
+ printf("Mode: %s Verbose: %s Tracing: %s\n",
+ mode, verbose ? "on" : "off", trace ? "on" : "off");
printf("Rexmt-interval: %d seconds, Max-timeout: %d seconds\n",
- rexmtval, maxtimeout);
+ rexmtval, maxtimeout);
}
void
@@ -555,7 +553,7 @@ intr(int signo)
char *
tail(char *filename)
{
- char *s;
+ char *s;
while (*filename) {
s = strrchr(filename, '/');
@@ -565,6 +563,7 @@ tail(char *filename)
return (s + 1);
*s = '\0';
}
+
return (filename);
}
@@ -574,7 +573,7 @@ tail(char *filename)
static __dead void
command(void)
{
- struct cmd *c;
+ struct cmd *c;
for (;;) {
printf("%s> ", prompt);
@@ -587,7 +586,7 @@ command(void)
if (margc == 0)
continue;
c = getcmd(margv[0]);
- if (c == (struct cmd *)-1) {
+ if (c == (struct cmd *) - 1) {
printf("?Ambiguous command\n");
continue;
}
@@ -602,9 +601,9 @@ command(void)
struct cmd *
getcmd(char *name)
{
- char *p, *q;
- struct cmd *c, *found;
- int nmatches, longest;
+ char *p, *q;
+ struct cmd *c, *found;
+ int nmatches, longest;
longest = 0;
nmatches = 0;
@@ -624,7 +623,8 @@ getcmd(char *name)
}
}
if (nmatches > 1)
- return ((struct cmd *)-1);
+ return ((struct cmd *) - 1);
+
return (found);
}
@@ -634,9 +634,9 @@ getcmd(char *name)
static int
makeargv(void)
{
- char *cp;
- char **argp = margv;
- int ret = 0;
+ char *cp;
+ char **argp = margv;
+ int ret = 0;
margc = 0;
for (cp = line; *cp;) {
@@ -658,6 +658,7 @@ makeargv(void)
*cp++ = '\0';
}
*argp++ = 0;
+
return (ret);
}
@@ -673,7 +674,7 @@ quit(int argc, char *argv[])
void
help(int argc, char *argv[])
{
- struct cmd *c;
+ struct cmd *c;
if (argc == 1) {
printf("Commands may be abbreviated. Commands are:\n\n");
@@ -685,7 +686,7 @@ help(int argc, char *argv[])
char *arg;
arg = *++argv;
c = getcmd(arg);
- if (c == (struct cmd *)-1)
+ if (c == (struct cmd *) - 1)
printf("?Ambiguous help command %s\n", arg);
else if (c == (struct cmd *)0)
printf("?Invalid help command %s\n", arg);
diff --git a/usr.bin/tftp/tftp.c b/usr.bin/tftp/tftp.c
index 5c6fc346ad3..6a4c8fc3e3e 100644
--- a/usr.bin/tftp/tftp.c
+++ b/usr.bin/tftp/tftp.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: tftp.c,v 1.16 2006/05/08 13:02:51 claudio Exp $ */
+/* $OpenBSD: tftp.c,v 1.17 2006/07/12 16:58:51 mglocker Exp $ */
/* $NetBSD: tftp.c,v 1.5 1995/04/29 05:55:25 cgd Exp $ */
/*
@@ -34,13 +34,14 @@
#if 0
static char sccsid[] = "@(#)tftp.c 8.1 (Berkeley) 6/6/93";
#endif
-static const char rcsid[] = "$OpenBSD: tftp.c,v 1.16 2006/05/08 13:02:51 claudio Exp $";
+static const char rcsid[] =
+ "$OpenBSD: tftp.c,v 1.17 2006/07/12 16:58:51 mglocker Exp $";
#endif /* not lint */
-/* Many bug fixes are from Jim Guyton <guyton@rand-unix> */
-
/*
* TFTP User Program -- Protocol Machines
+ *
+ * This version includes many modifications by Jim Guyton <guyton@rand-unix>
*/
#include <sys/types.h>
@@ -50,6 +51,7 @@ static const char rcsid[] = "$OpenBSD: tftp.c,v 1.16 2006/05/08 13:02:51 claudio
#include <netinet/in.h>
#include <arpa/tftp.h>
+#include <err.h>
#include <errno.h>
#include <poll.h>
#include <signal.h>
@@ -57,29 +59,35 @@ static const char rcsid[] = "$OpenBSD: tftp.c,v 1.16 2006/05/08 13:02:51 claudio
#include <stddef.h>
#include <string.h>
#include <unistd.h>
-#include <err.h>
#include "extern.h"
#include "tftpsubs.h"
#define PKTSIZE SEGSIZE + 4
-extern struct sockaddr_in peeraddr; /* filled in by main */
-extern int f; /* the opened socket */
-extern int trace;
-extern int verbose;
-extern int rexmtval;
-extern int maxtimeout;
-extern FILE *file;
-extern volatile sig_atomic_t intrflag;
+static int makerequest(int, const char *, struct tftphdr *, const char *);
+static void nak(int);
+static void tpacket(const char *, struct tftphdr *, int);
+static void startclock(void);
+static void stopclock(void);
+static void printstats(const char *, unsigned long);
+static void printtimeout(void);
-char ackbuf[PKTSIZE];
+extern struct sockaddr_in peeraddr; /* filled in by main */
+extern int f; /* the opened socket */
+extern int trace;
+extern int verbose;
+extern int rexmtval;
+extern int maxtimeout;
+extern FILE *file;
+extern volatile sig_atomic_t intrflag;
+char ackbuf[PKTSIZE];
struct timeval tstart;
struct timeval tstop;
struct errmsg {
- int e_code;
+ int e_code;
char *e_msg;
} errmsgs[] = {
{ EUNDEF, "Undefined error code" },
@@ -93,26 +101,18 @@ struct errmsg {
{ -1, NULL }
};
-static int makerequest(int, const char *, struct tftphdr *, const char *);
-static void nak(int);
-static void tpacket(const char *, struct tftphdr *, int);
-static void startclock(void);
-static void stopclock(void);
-static void printstats(const char *, unsigned long);
-static void printtimeout(void);
-
/*
* Send the requested file.
*/
void
sendfile(int fd, char *name, char *mode)
{
- struct tftphdr *dp, *ap; /* data and ack packets */
- struct sockaddr_in from;
- struct pollfd pfd[1];
- unsigned long amount;
- int convert; /* true if converting crlf -> lf */
- int n, nfds, error, fromlen, timeouts, block, size;
+ struct tftphdr *dp, *ap; /* data and ack packets */
+ struct sockaddr_in from;
+ struct pollfd pfd[1];
+ unsigned long amount;
+ int convert; /* true if converting crlf -> lf */
+ int n, nfds, error, fromlen, timeouts, block, size;
startclock(); /* start stat's clock */
dp = r_init(); /* reset fillbuf/read-ahead code */
@@ -230,12 +230,13 @@ abort:
void
recvfile(int fd, char *name, char *mode)
{
- struct tftphdr *dp, *ap; /* data and ack packets */
- struct sockaddr_in from;
- struct pollfd pfd[1];
- unsigned long amount;
- int convert; /* true if converting crlf -> lf */
- int n, nfds, error, fromlen, timeouts, block, size, firsttrip;
+ struct tftphdr *dp, *ap; /* data and ack packets */
+ struct sockaddr_in from;
+ struct pollfd pfd[1];
+ unsigned long amount;
+ int convert; /* true if converting crlf -> lf */
+ int n, nfds, error, fromlen, timeouts, block, size;
+ int firsttrip;
startclock(); /* start stat's clock */
dp = w_init(); /* reset fillbuf/read-ahead code */
@@ -345,7 +346,7 @@ abort:
/* ok to ack, since user has seen err msg */
ap->th_opcode = htons((u_short)ACK);
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));
write_behind(file, convert); /* flush last buffer */
@@ -362,8 +363,8 @@ static int
makerequest(int request, const char *name, struct tftphdr *tp,
const char *mode)
{
- char *cp;
- int len, pktlen;
+ char *cp;
+ int len, pktlen;
tp->th_opcode = htons((u_short)request);
cp = tp->th_stuff;
@@ -372,6 +373,7 @@ makerequest(int request, const char *name, struct tftphdr *tp,
strlcpy(cp, name, pktlen);
strlcpy(cp + len, mode, pktlen - len);
len += strlen(mode) + 1;
+
return (cp + len - (char *)tp);
}
@@ -384,9 +386,9 @@ makerequest(int request, const char *name, struct tftphdr *tp,
static void
nak(int error)
{
- struct errmsg *pe;
- struct tftphdr *tp;
- int length;
+ struct errmsg *pe;
+ struct tftphdr *tp;
+ int length;
tp = (struct tftphdr *)ackbuf;
tp->th_opcode = htons((u_short)ERROR);
@@ -411,9 +413,10 @@ nak(int error)
static void
tpacket(const char *s, struct tftphdr *tp, int n)
{
- static char *opcodes[] =
+ char *cp, *file;
+ static char *opcodes[] =
{ "#0", "RRQ", "WRQ", "DATA", "ACK", "ERROR" };
- char *cp, *file;
+
u_short op = ntohs(tp->th_opcode);
if (op < RRQ || op > ERROR)
@@ -444,19 +447,19 @@ tpacket(const char *s, struct tftphdr *tp, int n)
static void
startclock(void)
{
- (void) gettimeofday(&tstart, NULL);
+ (void)gettimeofday(&tstart, NULL);
}
static void
stopclock(void)
{
- (void) gettimeofday(&tstop, NULL);
+ (void)gettimeofday(&tstop, NULL);
}
static void
printstats(const char *direction, unsigned long amount)
{
- double delta;
+ double delta;
/* compute delta in 1/10's second units */
delta = ((tstop.tv_sec * 10.) + (tstop.tv_usec / 100000)) -
diff --git a/usr.bin/tftp/tftpsubs.c b/usr.bin/tftp/tftpsubs.c
index 5c23d0fd32e..f8a00a79301 100644
--- a/usr.bin/tftp/tftpsubs.c
+++ b/usr.bin/tftp/tftpsubs.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: tftpsubs.c,v 1.9 2003/09/24 20:21:40 deraadt Exp $ */
+/* $OpenBSD: tftpsubs.c,v 1.10 2006/07/12 16:58:51 mglocker Exp $ */
/* $NetBSD: tftpsubs.c,v 1.3 1994/12/08 09:51:31 jtc Exp $ */
/*
@@ -34,23 +34,26 @@
#if 0
static char sccsid[] = "@(#)tftpsubs.c 8.1 (Berkeley) 6/6/93";
#endif
-static const char rcsid[] = "$OpenBSD: tftpsubs.c,v 1.9 2003/09/24 20:21:40 deraadt Exp $";
+static const char rcsid[] =
+ "$OpenBSD: tftpsubs.c,v 1.10 2006/07/12 16:58:51 mglocker Exp $";
#endif /* not lint */
-/* Simple minded read-ahead/write-behind subroutines for tftp user and
- server. Written originally with multiple buffers in mind, but current
- implementation has two buffer logic wired in.
-
- Todo: add some sort of final error check so when the write-buffer
- is finally flushed, the caller can detect if the disk filled up
- (or had an i/o error) and return a nak to the other side.
-
- Jim Guyton 10/85
+/*
+ * Simple minded read-ahead/write-behind subroutines for tftp user and
+ * server. Written originally with multiple buffers in mind, but current
+ * implementation has two buffer logic wired in.
+ *
+ * Todo: add some sort of final error check so when the write-buffer
+ * is finally flushed, the caller can detect if the disk filled up
+ * (or had an i/o error) and return a nak to the other side.
+ *
+ * Jim Guyton 10/85
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
+
#include <netinet/in.h>
#include <arpa/tftp.h>
@@ -59,90 +62,92 @@ static const char rcsid[] = "$OpenBSD: tftpsubs.c,v 1.9 2003/09/24 20:21:40 dera
#include "tftpsubs.h"
-#define PKTSIZE SEGSIZE+4 /* should be moved to tftp.h */
+#define PKTSIZE SEGSIZE + 4 /* should be moved to tftp.h */
+ /* values for bf.counter */
+#define BF_ALLOC -3 /* alloc'd but not yet filled */
+#define BF_FREE -2 /* free */
+/* [-1 .. SEGSIZE] = size of data in the data buffer */
+
+static struct tftphdr *rw_init(int);
struct bf {
- int counter; /* size of data in buffer, or flag */
- char buf[PKTSIZE]; /* room for data packet */
+ int counter; /* size of data in buffer, or flag */
+ char buf[PKTSIZE]; /* room for data packet */
} bfs[2];
- /* Values for bf.counter */
-#define BF_ALLOC -3 /* alloc'd but not yet filled */
-#define BF_FREE -2 /* free */
-/* [-1 .. SEGSIZE] = size of data in the data buffer */
-
-static int nextone; /* index of next buffer to use */
-static int current; /* index of buffer in use */
-
+static int nextone; /* index of next buffer to use */
+static int current; /* index of buffer in use */
/* control flags for crlf conversions */
-int newline = 0; /* fillbuf: in middle of newline expansion */
-int prevchar = -1; /* putbuf: previous char (cr check) */
-
-static struct tftphdr *rw_init(int);
+int newline = 0; /* fillbuf: in middle of newline expansion */
+int prevchar = -1; /* putbuf: previous char (cr check) */
struct tftphdr *
w_init(void)
{
- return rw_init(0); /* write-behind */
+ return (rw_init(0)); /* write-behind */
}
struct tftphdr *
r_init(void)
{
- return rw_init(1); /* read-ahead */
+ return (rw_init(1)); /* read-ahead */
}
-/* init for either read-ahead or write-behind */
-/* zero for write-behind, one for read-head */
+/*
+ * Init for either read-ahead or write-behind.
+ * Zero for write-behind, one for read-head.
+ */
static struct tftphdr *
rw_init(int x)
{
- newline = 0; /* init crlf flag */
+ newline = 0; /* init crlf flag */
prevchar = -1;
- bfs[0].counter = BF_ALLOC; /* pass out the first buffer */
+ bfs[0].counter = BF_ALLOC; /* pass out the first buffer */
current = 0;
bfs[1].counter = BF_FREE;
- nextone = x; /* ahead or behind? */
- return (struct tftphdr *)bfs[0].buf;
-}
+ nextone = x; /* ahead or behind? */
+ 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.
+/*
+ * 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)
{
- struct bf *b;
+ struct bf *b;
+
+ bfs[current].counter = BF_FREE; /* free old one */
+ current = !current; /* "incr" current */
- bfs[current].counter = BF_FREE; /* free old one */
- current = !current; /* "incr" current */
+ b = &bfs[current]; /* look at new buffer */
+ if (b->counter == BF_FREE) /* if it's empty */
+ read_ahead(file, convert); /* fill it */
+ /* assert(b->counter != BF_FREE); */ /* check */
+ *dpp = (struct tftphdr *)b->buf; /* set caller's ptr */
- 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 */
- *dpp = (struct tftphdr *)b->buf; /* set caller's ptr */
- return b->counter;
+ return (b->counter);
}
/*
- * fill the input buffer, doing ascii conversions if requested
- * conversions are lf -> cr,lf and cr -> cr, nul
+ * 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)
{
- int i;
- char *p;
- int c;
- struct bf *b;
- struct tftphdr *dp;
-
- b = &bfs[nextone]; /* look at "next" buffer */
- if (b->counter != BF_FREE) /* nop if not free */
+ int i;
+ char *p;
+ int c;
+ struct bf *b;
+ struct tftphdr *dp;
+
+ b = &bfs[nextone]; /* look at "next" buffer */
+ if (b->counter != BF_FREE) /* nop if not free */
return;
- nextone = !nextone; /* "incr" next buffer ptr */
+ nextone = !nextone; /* "incr" next buffer ptr */
dp = (struct tftphdr *)b->buf;
@@ -152,16 +157,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 */
+ c = '\n'; /* lf to cr, lf */
else
- c = '\0'; /* cr to cr,nul */
+ c = '\0'; /* cr to cr, nul */
newline = 0;
} else {
c = getc(file);
- if (c == EOF) break;
+ if (c == EOF)
+ break;
if (c == '\n' || c == '\r') {
prevchar = c;
c = '\r';
@@ -173,74 +179,80 @@ read_ahead(FILE *file, int convert)
b->counter = (int)(p - dp->th_data);
}
-/* Update count associated with the buffer, get new buffer
- from the queue. Calls write_behind only if next buffer not
- available.
+/*
+ * Update count associated with the buffer, get new buffer
+ * from the queue. Calls write_behind only if next buffer not
+ * available.
*/
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 */
- if (bfs[current].counter != BF_FREE) /* if not free */
- (void)write_behind(file, convert); /* flush it */
- bfs[current].counter = BF_ALLOC; /* mark as alloc'd */
+ bfs[current].counter = ct; /* set size of data to write */
+ current = !current; /* switch to other buffer */
+ if (bfs[current].counter != BF_FREE) /* if not free */
+ /* flush it */
+ (void)write_behind(file, convert);
+ bfs[current].counter = BF_ALLOC; /* mark as alloc'd */
*dpp = (struct tftphdr *)bfs[current].buf;
- return ct; /* this is a lie of course */
+
+ return (ct); /* this is a lie of course */
}
/*
* Output a buffer to a file, converting from netascii if requested.
- * CR,NUL -> CR and CR,LF => LF.
+ * CR, NUL -> CR and CR, LF -> LF.
* Note spec is undefined if we get CR as last byte of file or a
* CR followed by anything else. In this case we leave it alone.
-n */
+ */
int
write_behind(FILE *file, int convert)
{
- char *buf;
- int count;
- int ct;
- char *p;
- int c; /* current character */
- struct bf *b;
- struct tftphdr *dp;
+ char *buf;
+ int count;
+ int ct;
+ char *p;
+ int c; /* current character */
+ struct bf *b;
+ struct tftphdr *dp;
b = &bfs[nextone];
- if (b->counter < -1) /* anything to flush? */
- return 0; /* just nop if nothing to do */
+ if (b->counter < -1) /* anything to flush? */
+ return (0); /* just nop if nothing to do */
- count = b->counter; /* remember byte count */
- b->counter = BF_FREE; /* reset flag */
+ count = b->counter; /* remember byte count */
+ b->counter = BF_FREE; /* reset flag */
dp = (struct tftphdr *)b->buf;
- nextone = !nextone; /* incr for next time */
+ nextone = !nextone; /* incr for next time */
buf = dp->th_data;
- if (count <= 0) return -1; /* nak logic? */
+ if (count <= 0) /* nak logic? */
+ return (-1);
if (convert == 0)
- return write(fileno(file), buf, count);
+ return (write(fileno(file), buf, count));
p = buf;
ct = count;
- while (ct--) { /* loop over the buffer */
- c = *p++; /* pick up a character */
- if (prevchar == '\r') { /* if prev char was cr */
- if (c == '\n') /* if have cr,lf then just */
- fseek(file, -1, 1); /* smash lf on top of the cr */
- else if (c == '\0') /* if have cr,nul then */
- goto skipit; /* just skip over the putc */
- /* else just fall through and allow it */
+ while (ct--) { /* loop over the buffer */
+ c = *p++; /* pick up a character */
+ if (prevchar == '\r') { /* if prev char was cr */
+ if (c == '\n') /* if have cr,lf then just */
+ /* smash lf on top of the cr */
+ fseek(file, -1, 1);
+ else if (c == '\0') /* if have cr,nul then */
+ goto skipit; /* just skip over the putc */
+ /* FALLTHROUGH */
}
putc(c, file);
skipit:
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
* response to packet N is really their response to packet N-1.
*
@@ -250,24 +262,22 @@ skipit:
* We return the number of packets we flushed (mostly for reporting
* when trace is active).
*/
-
int
synchnet(int f)
{
- int i, j = 0;
- char rbuf[PKTSIZE];
- struct sockaddr_in from;
- socklen_t fromlen;
+ int i, j = 0;
+ char rbuf[PKTSIZE];
+ struct sockaddr_in from;
+ socklen_t fromlen;
- while (1) {
- (void) ioctl(f, FIONREAD, &i);
+ for (;;) {
+ (void)ioctl(f, FIONREAD, &i);
if (i) {
j++;
- fromlen = sizeof from;
- (void) recvfrom(f, rbuf, sizeof (rbuf), 0,
+ fromlen = sizeof(from);
+ (void)recvfrom(f, rbuf, sizeof(rbuf), 0,
(struct sockaddr *)&from, &fromlen);
- } else {
- return(j);
- }
+ } else
+ return (j);
}
}
diff --git a/usr.bin/tftp/tftpsubs.h b/usr.bin/tftp/tftpsubs.h
index fe2e4103891..bd6f55adf13 100644
--- a/usr.bin/tftp/tftpsubs.h
+++ b/usr.bin/tftp/tftpsubs.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: tftpsubs.h,v 1.4 2003/06/03 02:56:18 millert Exp $ */
+/* $OpenBSD: tftpsubs.h,v 1.5 2006/07/12 16:58:51 mglocker Exp $ */
/* $NetBSD: tftpsubs.h,v 1.2 1994/12/08 09:51:32 jtc Exp $ */
/*
@@ -36,12 +36,12 @@
* Prototypes for read-ahead/write-behind subroutines for tftp user and
* server.
*/
-struct tftphdr *r_init(void);
-void read_ahead(FILE *, int);
-int readit(FILE *, struct tftphdr **, int);
+struct tftphdr *r_init(void);
+void read_ahead(FILE *, int);
+int readit(FILE *, struct tftphdr **, int);
-int synchnet(int);
+int synchnet(int);
-struct tftphdr *w_init(void);
-int write_behind(FILE *, int);
-int writeit(FILE *, struct tftphdr **, int, int);
+struct tftphdr *w_init(void);
+int write_behind(FILE *, int);
+int writeit(FILE *, struct tftphdr **, int, int);