Apply some code cleanups that apparently fix Solaris 7 gcc problems.

This commit is contained in:
hpa 2003-04-12 06:54:58 +00:00
parent 8796832723
commit 571deaa36b
3 changed files with 93 additions and 79 deletions

View file

@ -1,5 +1,10 @@
$Id$ $Id$
Changes in 0.34:
Additional Solaris gcc compiler bug workarounds; these
actually make the code somewhat cleaner.
Changes in 0.33: Changes in 0.33:
Even better error messages. Even better error messages.

View file

@ -89,6 +89,7 @@ tftp_sendfile(int fd, const char *name, const char *mode)
struct sockaddr_in from; struct sockaddr_in from;
int fromlen; int fromlen;
FILE *file; FILE *file;
u_short ap_opcode, ap_block;
startclock(); /* start stat's clock */ startclock(); /* start stat's clock */
dp = r_init(); /* reset fillbuf/read-ahead code */ dp = r_init(); /* reset fillbuf/read-ahead code */
@ -141,17 +142,17 @@ 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->th_opcode = ntohs(ap->th_opcode); ap_opcode = ntohs((u_short)ap->th_opcode);
ap->th_block = ntohs(ap->th_block); ap_block = ntohs((u_short)ap->th_block);
if (ap->th_opcode == ERROR) { if (ap_opcode == ERROR) {
printf("Error code %d: %s\n", ap->th_code, printf("Error code %d: %s\n", ap_block,
ap->th_msg); ap->th_msg);
goto abort; goto abort;
} }
if (ap->th_opcode == ACK) { if (ap_opcode == ACK) {
int j; int j;
if (ap->th_block == block) { if (ap_block == block) {
break; break;
} }
/* On an error, try to synchronize /* On an error, try to synchronize
@ -197,6 +198,7 @@ tftp_recvfile(int fd, const char *name, const char *mode)
int fromlen; int fromlen;
FILE *file; FILE *file;
volatile int convert; /* true if converting crlf -> lf */ volatile int convert; /* true if converting crlf -> lf */
u_short dp_opcode, dp_block;
startclock(); startclock();
dp = w_init(); dp = w_init();
@ -214,7 +216,7 @@ tftp_recvfile(int fd, const char *name, const char *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++;
} }
@ -246,17 +248,16 @@ send_ack:
if (trace) if (trace)
tpacket("received", dp, n); tpacket("received", dp, n);
/* should verify client address */ /* should verify client address */
dp->th_opcode = ntohs(dp->th_opcode); dp_opcode = ntohs((u_short)dp->th_opcode);
dp->th_block = ntohs(dp->th_block); dp_block = ntohs((u_short)dp->th_block);
if (dp->th_opcode == ERROR) { if (dp_opcode == ERROR) {
printf("Error code %d: %s\n", dp->th_code, printf("Error code %d: %s\n", dp_block, dp->th_msg);
dp->th_msg); goto abort;
goto abort;
} }
if (dp->th_opcode == DATA) { if (dp_opcode == DATA) {
int j; int j;
if (dp->th_block == block) { if (dp_block == block) {
break; /* have next packet */ break; /* have next packet */
} }
/* On an error, try to synchronize /* On an error, try to synchronize
@ -266,7 +267,7 @@ send_ack:
if (j && trace) { if (j && trace) {
printf("discarded %d packets\n", j); printf("discarded %d packets\n", j);
} }
if (dp->th_block == (block-1)) { if (dp_block == (block-1)) {
goto send_ack; /* resend ack */ goto send_ack; /* resend ack */
} }
} }
@ -308,21 +309,19 @@ makerequest(int request, const char *name,
return (cp - (char *)tp); return (cp - (char *)tp);
} }
struct errmsg { static const char * const errmsgs[] =
int e_code; {
const char *e_msg; "Undefined error code", /* 0 - EUNDEF */
} errmsgs[] = { "File not found", /* 1 - ENOTFOUND */
{ EUNDEF, "Undefined error code" }, "Access denied", /* 2 - EACCESS */
{ ENOTFOUND, "File not found" }, "Disk full or allocation exceeded", /* 3 - ENOSPACE */
{ EACCESS, "Access violation" }, "Illegal TFTP operation", /* 4 - EBADOP */
{ ENOSPACE, "Disk full or allocation exceeded" }, "Unknown transfer ID", /* 5 - EBADID */
{ EBADOP, "Illegal TFTP operation" }, "File already exists", /* 6 - EEXISTS */
{ EBADID, "Unknown transfer ID" }, "No such user", /* 7 - ENOUSER */
{ EEXISTS, "File already exists" }, "Failure to negotiate RFC2347 options" /* 8 - EOPTNEG */
{ ENOUSER, "No such user" },
{ EOPTNEG, "Failure to negotiate RFC2347 options" },
{ -1, 0 }
}; };
#define ERR_CNT (sizeof(errmsgs)/sizeof(const char *))
/* /*
* Send a nak packet (error message). * Send a nak packet (error message).
@ -333,26 +332,28 @@ struct errmsg {
static void static void
nak(int error, const char *msg) nak(int error, const char *msg)
{ {
struct errmsg *pe;
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 ( !msg ) {
if ( error >= 100 ) { if ( error >= 100 ) {
/* This is a Unix errno+100 */
if ( !msg )
msg = strerror(error - 100); msg = strerror(error - 100);
tp->th_code = EUNDEF; error = EUNDEF;
} else { } else {
for (pe = errmsgs; pe->e_code >= 0; pe++) { if ( (unsigned)error >= ERR_CNT )
if (pe->e_code == error) { error = EUNDEF;
msg = pe->e_msg;
break; if ( !msg )
} msg = errmsgs[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 */
@ -370,7 +371,7 @@ 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(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);

View file

@ -270,6 +270,7 @@ main(int argc, char **argv)
#ifdef WITH_REGEX #ifdef WITH_REGEX
char *rewrite_file = NULL; char *rewrite_file = NULL;
#endif #endif
u_short tp_opcode;
/* basename() is way too much of a pain from a portability standpoint */ /* basename() is way too much of a pain from a portability standpoint */
@ -677,8 +678,8 @@ main(int argc, char **argv)
exit(EX_IOERR); exit(EX_IOERR);
} }
tp = (struct tftphdr *)buf; tp = (struct tftphdr *)buf;
tp->th_opcode = ntohs(tp->th_opcode); tp_opcode = ntohs(tp->th_opcode);
if (tp->th_opcode == RRQ || tp->th_opcode == WRQ) if (tp_opcode == RRQ || tp_opcode == WRQ)
tftp(tp, n); tftp(tp, n);
exit(0); exit(0);
} }
@ -713,11 +714,12 @@ tftp(struct tftphdr *tp, int size)
char *origfilename; char *origfilename;
char *filename, *mode = NULL; char *filename, *mode = NULL;
const char *errmsgptr; const char *errmsgptr;
u_short tp_opcode = ntohs(tp->th_opcode);
char *val = NULL, *opt = NULL; char *val = NULL, *opt = NULL;
char *ap = ackbuf + 2; char *ap = ackbuf + 2;
((struct tftphdr *)ackbuf)->th_opcode = ntohs(OACK); ((struct tftphdr *)ackbuf)->th_opcode = htons(OACK);
origfilename = cp = (char *) &(tp->th_stuff); origfilename = cp = (char *) &(tp->th_stuff);
argn = 0; argn = 0;
@ -749,21 +751,21 @@ tftp(struct tftphdr *tp, int size)
exit(0); exit(0);
} }
if ( !(filename = if ( !(filename =
(*pf->f_rewrite)(origfilename, tp->th_opcode, &errmsgptr)) ) { (*pf->f_rewrite)(origfilename, tp_opcode, &errmsgptr)) ) {
nak(EACCESS, errmsgptr); /* File denied by mapping rule */ nak(EACCESS, errmsgptr); /* File denied by mapping rule */
exit(0); exit(0);
} }
if ( verbosity >= 1 ) { if ( verbosity >= 1 ) {
if ( filename == origfilename || !strcmp(filename, origfilename) ) if ( filename == origfilename || !strcmp(filename, origfilename) )
syslog(LOG_NOTICE, "%s from %s filename %s\n", syslog(LOG_NOTICE, "%s from %s filename %s\n",
tp->th_opcode == WRQ ? "WRQ" : "RRQ", tp_opcode == WRQ ? "WRQ" : "RRQ",
inet_ntoa(from.sin_addr), filename); inet_ntoa(from.sin_addr), filename);
else else
syslog(LOG_NOTICE, "%s from %s filename %s remapped to %s\n", syslog(LOG_NOTICE, "%s from %s filename %s remapped to %s\n",
tp->th_opcode == WRQ ? "WRQ" : "RRQ", tp_opcode == WRQ ? "WRQ" : "RRQ",
inet_ntoa(from.sin_addr), origfilename, filename); inet_ntoa(from.sin_addr), origfilename, filename);
} }
ecode = (*pf->f_validate)(filename, tp->th_opcode, pf, &errmsgptr); ecode = (*pf->f_validate)(filename, tp_opcode, pf, &errmsgptr);
if (ecode) { if (ecode) {
nak(ecode, errmsgptr); nak(ecode, errmsgptr);
exit(0); exit(0);
@ -783,12 +785,12 @@ tftp(struct tftphdr *tp, int size)
} }
if ( ap != (ackbuf+2) ) { if ( ap != (ackbuf+2) ) {
if ( tp->th_opcode == WRQ ) if ( tp_opcode == WRQ )
(*pf->f_recv)(pf, (struct tftphdr *)ackbuf, ap-ackbuf); (*pf->f_recv)(pf, (struct tftphdr *)ackbuf, ap-ackbuf);
else else
(*pf->f_send)(pf, (struct tftphdr *)ackbuf, ap-ackbuf); (*pf->f_send)(pf, (struct tftphdr *)ackbuf, ap-ackbuf);
} else { } else {
if (tp->th_opcode == WRQ) if (tp_opcode == WRQ)
(*pf->f_recv)(pf, NULL, 0); (*pf->f_recv)(pf, NULL, 0);
else else
(*pf->f_send)(pf, NULL, 0); (*pf->f_send)(pf, NULL, 0);
@ -1153,6 +1155,7 @@ tftp_sendfile(struct formats *pf, struct tftphdr *oap, int oacklen)
struct tftphdr *dp; struct tftphdr *dp;
struct tftphdr *ap; /* ack packet */ struct tftphdr *ap; /* ack packet */
static u_short block = 1; /* Static to avoid longjmp funnies */ static u_short block = 1; /* Static to avoid longjmp funnies */
u_short ap_opcode, ap_block;
int size, n; int size, n;
if (oap) { if (oap) {
@ -1170,15 +1173,15 @@ tftp_sendfile(struct formats *pf, struct tftphdr *oap, int oacklen)
goto abort; goto abort;
} }
ap = (struct tftphdr *)ackbuf; ap = (struct tftphdr *)ackbuf;
ap->th_opcode = ntohs((u_short)ap->th_opcode); ap_opcode = ntohs((u_short)ap->th_opcode);
ap->th_block = ntohs((u_short)ap->th_block); ap_block = ntohs((u_short)ap->th_block);
if (ap->th_opcode == ERROR) { if (ap_opcode == ERROR) {
syslog(LOG_WARNING, "tftp: client does not accept options\n"); syslog(LOG_WARNING, "tftp: client does not accept options\n");
goto abort; goto abort;
} }
if (ap->th_opcode == ACK) { if (ap_opcode == ACK) {
if (ap->th_block == 0) if (ap_block == 0)
break; break;
/* Resynchronize with the other side */ /* Resynchronize with the other side */
(void)synchnet(peer); (void)synchnet(peer);
@ -1211,14 +1214,14 @@ tftp_sendfile(struct formats *pf, struct tftphdr *oap, int oacklen)
goto abort; goto abort;
} }
ap = (struct tftphdr *)ackbuf; ap = (struct tftphdr *)ackbuf;
ap->th_opcode = ntohs((u_short)ap->th_opcode); ap_opcode = ntohs((u_short)ap->th_opcode);
ap->th_block = ntohs((u_short)ap->th_block); ap_block = ntohs((u_short)ap->th_block);
if (ap->th_opcode == ERROR) if (ap_opcode == ERROR)
goto abort; goto abort;
if (ap->th_opcode == ACK) { if (ap_opcode == ACK) {
if (ap->th_block == block) { if (ap_block == block) {
break; break;
} }
/* Re-synchronize with the other side */ /* Re-synchronize with the other side */
@ -1258,6 +1261,7 @@ tftp_recvfile(struct formats *pf, struct tftphdr *oap, int oacklen)
static struct tftphdr *ap; /* ack buffer */ static struct tftphdr *ap; /* ack buffer */
static u_short block = 0; static u_short block = 0;
static int acksize; static int acksize;
u_short dp_opcode, dp_block;
dp = w_init(); dp = w_init();
do { do {
@ -1286,17 +1290,17 @@ tftp_recvfile(struct formats *pf, struct tftphdr *oap, int oacklen)
syslog(LOG_WARNING, "tftpd: read: %m"); syslog(LOG_WARNING, "tftpd: read: %m");
goto abort; goto abort;
} }
dp->th_opcode = ntohs((u_short)dp->th_opcode); dp_opcode = ntohs((u_short)dp->th_opcode);
dp->th_block = ntohs((u_short)dp->th_block); dp_block = ntohs((u_short)dp->th_block);
if (dp->th_opcode == ERROR) if (dp_opcode == ERROR)
goto abort; goto abort;
if (dp->th_opcode == DATA) { if (dp_opcode == DATA) {
if (dp->th_block == block) { if (dp_block == block) {
break; /* normal */ break; /* normal */
} }
/* Re-synchronize with the other side */ /* Re-synchronize with the other side */
(void) synchnet(peer); (void) synchnet(peer);
if (dp->th_block == (block-1)) if (dp_block == (block-1))
goto send_ack; /* rexmit */ goto send_ack; /* rexmit */
} }
} }
@ -1320,8 +1324,8 @@ tftp_recvfile(struct formats *pf, struct tftphdr *oap, int oacklen)
timeout_quit = 0; timeout_quit = 0;
if (n >= 4 && /* if read some data */ if (n >= 4 && /* if read some data */
dp->th_opcode == DATA && /* and got a data block */ dp_opcode == DATA && /* and got a data block */
block == dp->th_block) { /* then my last ack was lost */ block == dp_block) { /* then my last ack was lost */
(void) send(peer, ackbuf, 4, 0); /* resend final ack */ (void) send(peer, ackbuf, 4, 0); /* resend final ack */
} }
abort: abort:
@ -1356,18 +1360,22 @@ nak(int error, const char *msg)
tp = (struct tftphdr *)buf; tp = (struct tftphdr *)buf;
tp->th_opcode = htons((u_short)ERROR); tp->th_opcode = htons((u_short)ERROR);
tp->th_code = htons((u_short)error);
if ( !msg ) { if ( error >= 100 ) {
if ( error >= 100 ) { /* This is a Unix errno+100 */
/* This is a Unix errno+100 */ if ( !msg )
msg = strerror(error - 100); msg = strerror(error - 100);
tp->th_code = EUNDEF; error = EUNDEF;
} else { } else {
if ( (unsigned)error >= ERR_CNT ) if ( (unsigned)error >= ERR_CNT )
tp->th_code = error = EUNDEF; error = EUNDEF;
if ( !msg )
msg = errmsgs[error]; msg = errmsgs[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 */