svn commit: r183992 - user/edwin/tftpd/libexec/tftpd
Edwin Groothuis
edwin at FreeBSD.org
Fri Oct 17 22:19:09 UTC 2008
Author: edwin
Date: Fri Oct 17 22:19:08 2008
New Revision: 183992
URL: http://svn.freebsd.org/changeset/base/183992
Log:
Add OP_ prefixes to packet types as defined in arpa/tftp.h.
Add TFTP_ prefixes to error codes as defined in arpa/tftp.h.
This all to avoid confusion (which is a good thing!)
Submitted by: netchild@, rink@ and others
Modified:
user/edwin/tftpd/libexec/tftpd/tftp-io.c
user/edwin/tftpd/libexec/tftpd/tftp-options.c
user/edwin/tftpd/libexec/tftpd/tftp-transfer.c
user/edwin/tftpd/libexec/tftpd/tftp-utils.c
user/edwin/tftpd/libexec/tftpd/tftp-utils.h
user/edwin/tftpd/libexec/tftpd/tftpd.c
Modified: user/edwin/tftpd/libexec/tftpd/tftp-io.c
==============================================================================
--- user/edwin/tftpd/libexec/tftpd/tftp-io.c Fri Oct 17 22:18:35 2008 (r183991)
+++ user/edwin/tftpd/libexec/tftpd/tftp-io.c Fri Oct 17 22:19:08 2008 (r183992)
@@ -57,16 +57,16 @@ struct errmsg {
int e_code;
const char *e_msg;
} errmsgs[] = {
- { EUNDEF, "Undefined error code" },
- { ENOTFOUND, "File not found" },
- { EACCESS, "Access violation" },
- { ENOSPACE, "Disk full or allocation exceeded" },
- { EBADOP, "Illegal TFTP operation" },
- { EBADID, "Unknown transfer ID" },
- { EEXISTS, "File already exists" },
- { ENOUSER, "No such user" },
- { EOPTNEG, "Option negotiation" },
- { -1, NULL }
+ { TFTP_EUNDEF, "Undefined error code" },
+ { TFTP_ENOTFOUND, "File not found" },
+ { TFTP_EACCESS, "Access violation" },
+ { TFTP_ENOSPACE, "Disk full or allocation exceeded" },
+ { TFTP_EBADOP, "Illegal TFTP operation" },
+ { TFTP_EBADID, "Unknown transfer ID" },
+ { TFTP_EEXISTS, "File already exists" },
+ { TFTP_ENOUSER, "No such user" },
+ { TFTP_EOPTNEG, "Option negotiation" },
+ { -1, NULL }
};
#define DROPPACKET(s) \
@@ -148,14 +148,14 @@ send_error(int peer, int error)
DROPPACKET("send_error");
tp = (struct tftphdr *)buf;
- tp->th_opcode = htons((u_short)ERROR);
+ tp->th_opcode = htons((u_short)OP_ERROR);
tp->th_code = htons((u_short)error);
for (pe = errmsgs; pe->e_code >= 0; pe++)
if (pe->e_code == error)
break;
if (pe->e_code < 0) {
pe->e_msg = strerror(error - 100);
- tp->th_code = EUNDEF; /* set 'undef' errorcode */
+ tp->th_code = TFTP_EUNDEF; /* set 'undef' errorcode */
}
strcpy(tp->th_msg, pe->e_msg);
length = strlen(pe->e_msg);
@@ -190,7 +190,7 @@ send_wrq(int peer, char *filename, char
DROPPACKETn("send_wrq", 1);
tp = (struct tftphdr *)buf;
- tp->th_opcode = htons((u_short)WRQ);
+ tp->th_opcode = htons((u_short)OP_WRQ);
size = 2;
bp = tp->th_stuff;
@@ -238,7 +238,7 @@ send_rrq(int peer, char *filename, char
DROPPACKETn("send_rrq", 1);
tp = (struct tftphdr *)buf;
- tp->th_opcode = htons((u_short)RRQ);
+ tp->th_opcode = htons((u_short)OP_RRQ);
size = 2;
bp = tp->th_stuff;
@@ -291,7 +291,7 @@ send_oack(int peer)
tp = (struct tftphdr *)buf;
bp = buf + 2;
size = sizeof(buf) - 2;
- tp->th_opcode = htons((u_short)OACK);
+ tp->th_opcode = htons((u_short)OP_OACK);
for (i = 0; options[i].o_type != NULL; i++) {
if (options[i].o_reply != NULL) {
n = snprintf(bp, size, "%s%c%s", options[i].o_type,
@@ -334,7 +334,7 @@ send_ack(int fp, uint16_t block)
tp = (struct tftphdr *)buf;
bp = buf + 2;
size = sizeof(buf) - 2;
- tp->th_opcode = htons((u_short)ACK);
+ tp->th_opcode = htons((u_short)OP_ACK);
tp->th_block = htons((u_short)block);
size = 4;
@@ -365,7 +365,7 @@ send_data(int peer, uint16_t block, char
pkt = (struct tftphdr *)buf;
- pkt->th_opcode = htons((u_short)DATA);
+ pkt->th_opcode = htons((u_short)OP_DATA);
pkt->th_block = htons((u_short)block);
memcpy(pkt->th_data, data, size);
@@ -449,11 +449,10 @@ receive_packet(int peer, char *data, int
}
pkt->th_opcode = ntohs((u_short)pkt->th_opcode);
- if (pkt->th_opcode == DATA ||
- pkt->th_opcode == ACK)
+ if (pkt->th_opcode == OP_DATA || pkt->th_opcode == OP_ACK)
pkt->th_block = ntohs((u_short)pkt->th_block);
- if (pkt->th_opcode == DATA && n > pktsize) {
+ if (pkt->th_opcode == OP_DATA && n > pktsize) {
tftp_log(LOG_ERR, "receive_packet: packet too big");
return (RP_TOOBIG);
}
@@ -465,7 +464,7 @@ receive_packet(int peer, char *data, int
return (RP_WRONGSOURCE);
}
- if (pkt->th_opcode == ERROR) {
+ if (pkt->th_opcode == OP_ERROR) {
tftp_log(LOG_ERR, "Got ERROR packet: %s", pkt->th_msg);
return (RP_ERROR);
}
Modified: user/edwin/tftpd/libexec/tftpd/tftp-options.c
==============================================================================
--- user/edwin/tftpd/libexec/tftpd/tftp-options.c Fri Oct 17 22:18:35 2008 (r183991)
+++ user/edwin/tftpd/libexec/tftpd/tftp-options.c Fri Oct 17 22:19:08 2008 (r183992)
@@ -75,7 +75,7 @@ int options_extra_enabled = 1;
* return rubbish.
* - The handler returns if there is a serious problem with the
* values submitted in the option.
- * - Sending the EBADOP packets is done by the handler.
+ * - Sending the TFTP_EBADOP packets is done by the handler.
*/
int
@@ -85,7 +85,7 @@ option_tsize(int peer, struct tftphdr *t
if (options[OPT_TSIZE].o_request == NULL)
return (0);
- if (mode == RRQ)
+ if (mode == OP_RRQ)
asprintf(&options[OPT_TSIZE].o_reply,
"%ju", stbuf->st_size);
else
@@ -108,7 +108,7 @@ option_timeout(int peer)
"Received bad value for timeout. "
"Should be between %d and %d, received %s",
TIMEOUT_MIN, TIMEOUT_MAX);
- send_error(peer, EBADOP);
+ send_error(peer, TFTP_EBADOP);
if (acting_as_client)
return (1);
exit(1);
@@ -141,7 +141,7 @@ option_rollover(int peer)
"ignoring request",
options[OPT_ROLLOVER].o_request);
if (acting_as_client) {
- send_error(peer, EBADOP);
+ send_error(peer, TFTP_EBADOP);
return (1);
}
return (0);
@@ -181,7 +181,7 @@ option_blksize(int peer)
tftp_log(LOG_ERR,
"Invalid blocksize (%d bytes), aborting",
size);
- send_error(peer, EBADOP);
+ send_error(peer, TFTP_EBADOP);
return (1);
} else {
tftp_log(LOG_WARNING,
@@ -197,7 +197,7 @@ option_blksize(int peer)
"Invalid blocksize (%d bytes), "
"net.inet.udp.maxdgram sysctl limits it to "
"%d bytes.\n", size, *maxdgram);
- send_error(peer, EBADOP);
+ send_error(peer, TFTP_EBADOP);
return (1);
} else {
tftp_log(LOG_WARNING,
Modified: user/edwin/tftpd/libexec/tftpd/tftp-transfer.c
==============================================================================
--- user/edwin/tftpd/libexec/tftpd/tftp-transfer.c Fri Oct 17 22:18:35 2008 (r183991)
+++ user/edwin/tftpd/libexec/tftpd/tftp-transfer.c Fri Oct 17 22:19:08 2008 (r183992)
@@ -109,7 +109,7 @@ tftp_send(int peer, uint16_t *block, str
rp_strerror(n_ack));
goto abort;
}
- if (rp->th_opcode == ACK) {
+ if (rp->th_opcode == OP_ACK) {
ts->blocks++;
if (rp->th_block == *block) {
ts->amount += size;
@@ -131,7 +131,7 @@ tftp_send(int peer, uint16_t *block, str
if (options[OPT_ROLLOVER].o_request == NULL) {
tftp_log(LOG_ERR,
"Block rollover but not allowed.");
- send_error(peer, EBADOP);
+ send_error(peer, TFTP_EBADOP);
gettimeofday(&(ts->tstop), NULL);
return;
}
@@ -198,7 +198,7 @@ tftp_receive(int peer, uint16_t *block,
if (options[OPT_ROLLOVER].o_request == NULL) {
tftp_log(LOG_ERR,
"Block rollover but not allowed.");
- send_error(peer, EBADOP);
+ send_error(peer, TFTP_EBADOP);
gettimeofday(&(ts->tstop), NULL);
return;
}
@@ -235,7 +235,7 @@ tftp_receive(int peer, uint16_t *block,
rp_strerror(n_data));
goto abort;
}
- if (rp->th_opcode == DATA) {
+ if (rp->th_opcode == OP_DATA) {
ts->blocks++;
if (rp->th_block == *block)
@@ -270,7 +270,7 @@ tftp_receive(int peer, uint16_t *block,
if (writesize < 0)
send_error(peer, errno + 100);
else
- send_error(peer, ENOSPACE);
+ send_error(peer, TFTP_ENOSPACE);
goto abort;
}
}
@@ -308,7 +308,7 @@ send_ack:
if (n_data <= 0)
break;
if (n_data > 0 &&
- rp->th_opcode == DATA && /* and got a data block */
+ rp->th_opcode == OP_DATA && /* and got a data block */
*block == rp->th_block) /* then my last ack was lost */
send_ack(peer, *block); /* resend final ack */
}
Modified: user/edwin/tftpd/libexec/tftpd/tftp-utils.c
==============================================================================
--- user/edwin/tftpd/libexec/tftpd/tftp-utils.c Fri Oct 17 22:18:35 2008 (r183991)
+++ user/edwin/tftpd/libexec/tftpd/tftp-utils.c Fri Oct 17 22:19:08 2008 (r183992)
@@ -112,7 +112,7 @@ get_field(int peer, char *buffer, ssize_
}
if (*cp != '\0') {
tftp_log(LOG_ERR, "Bad option - no trailing \\0 found");
- send_error(peer, EBADOP);
+ send_error(peer, TFTP_EBADOP);
exit(1);
}
return (cp - buffer + 1);
@@ -161,12 +161,12 @@ tftp_log(int priority, const char *messa
* Packet types
*/
struct packettypes packettypes[] = {
- { RRQ, "RRQ" },
- { WRQ, "WRQ" },
- { DATA, "DATA" },
- { ACK, "ACK" },
- { ERROR, "ERROR" },
- { OACK, "OACK" },
+ { OP_RRQ, "RRQ" },
+ { OP_WRQ, "WRQ" },
+ { OP_DATA, "DATA" },
+ { OP_ACK, "ACK" },
+ { OP_ERROR, "ERROR" },
+ { OP_OACK, "OACK" },
{ 0, NULL },
};
Modified: user/edwin/tftpd/libexec/tftpd/tftp-utils.h
==============================================================================
--- user/edwin/tftpd/libexec/tftpd/tftp-utils.h Fri Oct 17 22:18:35 2008 (r183991)
+++ user/edwin/tftpd/libexec/tftpd/tftp-utils.h Fri Oct 17 22:19:08 2008 (r183992)
@@ -122,3 +122,22 @@ struct tftp_stats {
void stats_init(struct tftp_stats *ts);
void printstats(const char *direction, int verbose, struct tftp_stats *ts);
+
+/* FreeBSD TFTP Specific Error codes (defined in arpa/tftp.h) */
+#define TFTP_EUNDEF EUNDEF
+#define TFTP_ENOTFOUND ENOTFOUND
+#define TFTP_EACCESS EACCESS
+#define TFTP_ENOSPACE ENOSPACE
+#define TFTP_EBADOP EBADOP
+#define TFTP_EBADID EBADID
+#define TFTP_EEXISTS EEXISTS
+#define TFTP_ENOUSER ENOUSER
+#define TFTP_EOPTNEG EOPTNEG
+
+/* FreeBSD TFTP Specific Error codes (defined in arpa/tftp.h) */
+#define OP_RRQ RRQ
+#define OP_WRQ WRQ
+#define OP_DATA DATA
+#define OP_ACK ACK
+#define OP_ERROR ERROR
+#define OP_OACK OACK
Modified: user/edwin/tftpd/libexec/tftpd/tftpd.c
==============================================================================
--- user/edwin/tftpd/libexec/tftpd/tftpd.c Fri Oct 17 22:18:35 2008 (r183991)
+++ user/edwin/tftpd/libexec/tftpd/tftpd.c Fri Oct 17 22:19:08 2008 (r183992)
@@ -406,7 +406,7 @@ main(int argc, char *argv[])
tp = (struct tftphdr *)recvbuffer;
tp->th_opcode = ntohs(tp->th_opcode);
- if (tp->th_opcode == RRQ) {
+ if (tp->th_opcode == OP_RRQ) {
if (allow_ro)
tftp_rrq(peer, tp->th_stuff, n - 1);
else {
@@ -415,7 +415,7 @@ main(int argc, char *argv[])
exit(1);
}
}
- if (tp->th_opcode == WRQ) {
+ if (tp->th_opcode == OP_WRQ) {
if (allow_wo)
tftp_wrq(peer, tp->th_stuff, n - 1);
else {
@@ -472,7 +472,7 @@ parse_header(int peer, char *recvbuffer,
i = get_field(peer, recvbuffer, size);
if (i >= PATH_MAX) {
tftp_log(LOG_ERR, "Bad option - filename too long");
- send_error(peer, EBADOP);
+ send_error(peer, TFTP_EBADOP);
exit(1);
}
*filename = recvbuffer;
@@ -493,7 +493,7 @@ parse_header(int peer, char *recvbuffer,
if (pf->f_mode == NULL) {
tftp_log(LOG_ERR,
"Bad option - Unknown transfer mode (%s)", *mode);
- send_error(peer, EBADOP);
+ send_error(peer, TFTP_EBADOP);
exit(1);
}
tftp_log(LOG_INFO, "Mode: '%s'", *mode);
@@ -526,7 +526,7 @@ tftp_wrq(int peer, char *recvbuffer, ssi
tftp_log(LOG_INFO, "Options found but not enabled");
}
- ecode = validate_access(peer, &filename, WRQ);
+ ecode = validate_access(peer, &filename, OP_WRQ);
if (ecode == 0) {
if (has_options)
send_oack(peer);
@@ -567,7 +567,7 @@ tftp_rrq(int peer, char *recvbuffer, ssi
tftp_log(LOG_INFO, "Options found but not enabled");
}
- ecode = validate_access(peer, &filename, RRQ);
+ ecode = validate_access(peer, &filename, OP_RRQ);
if (ecode == 0) {
if (has_options) {
int n;
@@ -583,7 +583,7 @@ tftp_rrq(int peer, char *recvbuffer, ssi
rp_strerror(n));
return;
}
- if (rp->th_opcode != ACK) {
+ if (rp->th_opcode != OP_ACK) {
if (debug&DEBUG_SIMPLE)
tftp_log(LOG_DEBUG,
"Expected ACK, got %s on OACK",
@@ -602,7 +602,8 @@ tftp_rrq(int peer, char *recvbuffer, ssi
* Avoid storms of naks to a RRQ broadcast for a relative
* bootfile pathname from a diskless Sun.
*/
- if (suppress_naks && *filename != '/' && ecode == ENOTFOUND)
+ if (suppress_naks && *filename != '/' &&
+ ecode == TFTP_ENOTFOUND)
exit(0);
tftp_log(LOG_ERR, "Prevent NAK storm");
send_error(peer, ecode);
@@ -635,7 +636,7 @@ find_next_name(char *filename, int *fd)
syslog(LOG_WARNING,
"Filename suffix too long (%d characters maximum)",
MAXPATHLEN);
- return (EACCESS);
+ return (TFTP_EACCESS);
}
/* Make sure the new filename is not too long */
@@ -643,7 +644,7 @@ find_next_name(char *filename, int *fd)
syslog(LOG_WARNING,
"Filename too long (%zd characters, %zd maximum)",
strlen(filename), MAXPATHLEN - len - 5);
- return (EACCESS);
+ return (TFTP_EACCESS);
}
/* Find the first file which doesn't exist */
@@ -657,7 +658,7 @@ find_next_name(char *filename, int *fd)
return 0;
}
- return (EEXIST);
+ return (TFTP_EEXISTS);
}
/*
@@ -685,7 +686,7 @@ validate_access(int peer, char **filep,
* Prevent tricksters from getting around the directory restrictions
*/
if (strstr(filename, "/../"))
- return (EACCESS);
+ return (TFTP_EACCESS);
if (*filename == '/') {
/*
@@ -702,17 +703,17 @@ validate_access(int peer, char **filep,
}
/* If directory list is empty, allow access to any file */
if (dirp->name == NULL && dirp != dirs)
- return (EACCESS);
+ return (TFTP_EACCESS);
if (stat(filename, &stbuf) < 0)
- return (errno == ENOENT ? ENOTFOUND : EACCESS);
+ return (errno == ENOENT ? TFTP_ENOTFOUND : TFTP_EACCESS);
if ((stbuf.st_mode & S_IFMT) != S_IFREG)
- return (ENOTFOUND);
- if (mode == RRQ) {
+ return (TFTP_ENOTFOUND);
+ if (mode == OP_RRQ) {
if ((stbuf.st_mode & S_IROTH) == 0)
- return (EACCESS);
+ return (TFTP_EACCESS);
} else {
if ((stbuf.st_mode & S_IWOTH) == 0)
- return (EACCESS);
+ return (TFTP_EACCESS);
}
} else {
int err;
@@ -724,14 +725,14 @@ validate_access(int peer, char **filep,
*/
if (!strncmp(filename, "../", 3))
- return (EACCESS);
+ return (TFTP_EACCESS);
/*
* If the file exists in one of the directories and isn't
* readable, continue looking. However, change the error code
* to give an indication that the file exists.
*/
- err = ENOTFOUND;
+ err = TFTP_ENOTFOUND;
for (dirp = dirs; dirp->name != NULL; dirp++) {
snprintf(pathname, sizeof(pathname), "%s/%s",
dirp->name, filename);
@@ -740,12 +741,12 @@ validate_access(int peer, char **filep,
if ((stbuf.st_mode & S_IROTH) != 0) {
break;
}
- err = EACCESS;
+ err = TFTP_EACCESS;
}
}
if (dirp->name != NULL)
*filep = filename = pathname;
- else if (mode == RRQ)
+ else if (mode == OP_RRQ)
return (err);
}
@@ -755,7 +756,7 @@ validate_access(int peer, char **filep,
*/
option_tsize(peer, NULL, mode, &stbuf);
- if (mode == RRQ)
+ if (mode == OP_RRQ)
fd = open(filename, O_RDONLY);
else {
if (create_new) {
@@ -773,7 +774,7 @@ validate_access(int peer, char **filep,
}
if (fd < 0)
return (errno + 100);
- file = fdopen(fd, (mode == RRQ)? "r":"w");
+ file = fdopen(fd, (mode == OP_RRQ)? "r":"w");
if (file == NULL) {
close(fd);
return (errno + 100);
More information about the svn-src-user
mailing list