libsa: cstyle cleanup tftp.c

No functinal changes intended.
This commit is contained in:
tsoome 2018-11-01 22:13:18 +00:00
parent c3ade4fe27
commit 40d6f96f88

View File

@ -63,30 +63,29 @@ __FBSDID("$FreeBSD$");
struct tftp_handle; struct tftp_handle;
struct tftprecv_extra; struct tftprecv_extra;
static ssize_t recvtftp(struct iodesc *d, void **pkt, void **payload, static ssize_t recvtftp(struct iodesc *, void **, void **, time_t, void *);
time_t tleft, void *recv_extra); static int tftp_open(const char *, struct open_file *);
static int tftp_open(const char *path, struct open_file *f); static int tftp_close(struct open_file *);
static int tftp_close(struct open_file *f); static int tftp_parse_oack(struct tftp_handle *, char *, size_t);
static int tftp_parse_oack(struct tftp_handle *h, char *buf, size_t len); static int tftp_read(struct open_file *, void *, size_t, size_t *);
static int tftp_read(struct open_file *f, void *buf, size_t size, size_t *resid); static off_t tftp_seek(struct open_file *, off_t, int);
static off_t tftp_seek(struct open_file *f, off_t offset, int where); static int tftp_set_blksize(struct tftp_handle *, const char *);
static int tftp_set_blksize(struct tftp_handle *h, const char *str); static int tftp_stat(struct open_file *, struct stat *);
static int tftp_stat(struct open_file *f, struct stat *sb);
struct fs_ops tftp_fsops = { struct fs_ops tftp_fsops = {
"tftp", .fs_name = "tftp",
tftp_open, .fo_open = tftp_open,
tftp_close, .fo_close = tftp_close,
tftp_read, .fo_read = tftp_read,
null_write, .fo_write = null_write,
tftp_seek, .fo_seek = tftp_seek,
tftp_stat, .fo_stat = tftp_stat,
null_readdir .fo_readdir = null_readdir
}; };
extern struct in_addr servip; extern struct in_addr servip;
static int tftpport = 2000; static int tftpport = 2000;
static int is_open = 0; static int is_open = 0;
/* /*
@ -94,21 +93,21 @@ static int is_open = 0;
* TFTP_REQUESTED_BLKSIZE of 1428 is (Ethernet MTU, less the TFTP, UDP and * TFTP_REQUESTED_BLKSIZE of 1428 is (Ethernet MTU, less the TFTP, UDP and
* IP header lengths). * IP header lengths).
*/ */
#define TFTP_REQUESTED_BLKSIZE 1428 #define TFTP_REQUESTED_BLKSIZE 1428
/* /*
* Choose a blksize big enough so we can test with Ethernet * Choose a blksize big enough so we can test with Ethernet
* Jumbo frames in the future. * Jumbo frames in the future.
*/ */
#define TFTP_MAX_BLKSIZE 9008 #define TFTP_MAX_BLKSIZE 9008
struct tftp_handle { struct tftp_handle {
struct iodesc *iodesc; struct iodesc *iodesc;
int currblock; /* contents of lastdata */ int currblock; /* contents of lastdata */
int islastblock; /* flag */ int islastblock; /* flag */
int validsize; int validsize;
int off; int off;
char *path; /* saved for re-requests */ char *path; /* saved for re-requests */
unsigned int tftp_blksize; unsigned int tftp_blksize;
unsigned long tftp_tsize; unsigned long tftp_tsize;
void *pkt; void *pkt;
@ -117,7 +116,7 @@ struct tftp_handle {
struct tftprecv_extra { struct tftprecv_extra {
struct tftp_handle *tftp_handle; struct tftp_handle *tftp_handle;
unsigned short rtype; /* Received type */ unsigned short rtype; /* Received type */
}; };
#define TFTP_MAX_ERRCODE EOPTNEG #define TFTP_MAX_ERRCODE EOPTNEG
@ -141,25 +140,25 @@ tftp_senderr(struct tftp_handle *h, u_short errcode, const char *msg)
{ {
struct { struct {
u_char header[HEADER_SIZE]; u_char header[HEADER_SIZE];
struct tftphdr t; struct tftphdr t;
u_char space[63]; /* +1 from t */ u_char space[63]; /* +1 from t */
} __packed __aligned(4) wbuf; } __packed __aligned(4) wbuf;
char *wtail; char *wtail;
int len; int len;
len = strlen(msg); len = strlen(msg);
if (len > sizeof(wbuf.space)) if (len > sizeof(wbuf.space))
len = sizeof(wbuf.space); len = sizeof(wbuf.space);
wbuf.t.th_opcode = htons((u_short) ERROR); wbuf.t.th_opcode = htons((u_short)ERROR);
wbuf.t.th_code = htons(errcode); wbuf.t.th_code = htons(errcode);
wtail = wbuf.t.th_msg; wtail = wbuf.t.th_msg;
bcopy(msg, wtail, len); bcopy(msg, wtail, len);
wtail[len] = '\0'; wtail[len] = '\0';
wtail += len + 1; wtail += len + 1;
sendudp(h->iodesc, &wbuf.t, wtail - (char *) &wbuf.t); sendudp(h->iodesc, &wbuf.t, wtail - (char *)&wbuf.t);
} }
static void static void
@ -169,14 +168,14 @@ tftp_sendack(struct tftp_handle *h, u_short block)
u_char header[HEADER_SIZE]; u_char header[HEADER_SIZE];
struct tftphdr t; struct tftphdr t;
} __packed __aligned(4) wbuf; } __packed __aligned(4) wbuf;
char *wtail; char *wtail;
wbuf.t.th_opcode = htons((u_short) ACK); wbuf.t.th_opcode = htons((u_short)ACK);
wtail = (char *) &wbuf.t.th_block; wtail = (char *)&wbuf.t.th_block;
wbuf.t.th_block = htons(block); wbuf.t.th_block = htons(block);
wtail += 2; wtail += 2;
sendudp(h->iodesc, &wbuf.t, wtail - (char *) &wbuf.t); sendudp(h->iodesc, &wbuf.t, wtail - (char *)&wbuf.t);
} }
static ssize_t static ssize_t
@ -190,7 +189,7 @@ recvtftp(struct iodesc *d, void **pkt, void **payload, time_t tleft,
ssize_t len; ssize_t len;
errno = 0; errno = 0;
extra = (struct tftprecv_extra *)recv_extra; extra = recv_extra;
h = extra->tftp_handle; h = extra->tftp_handle;
len = readudp(d, &ptr, (void **)&t, tleft); len = readudp(d, &ptr, (void **)&t, tleft);
@ -205,7 +204,7 @@ recvtftp(struct iodesc *d, void **pkt, void **payload, time_t tleft,
case DATA: { case DATA: {
int got; int got;
if (htons(t->th_block) < (u_short) d->xid) { if (htons(t->th_block) < (u_short)d->xid) {
/* /*
* Apparently our ACK was missed, re-send. * Apparently our ACK was missed, re-send.
*/ */
@ -213,7 +212,7 @@ recvtftp(struct iodesc *d, void **pkt, void **payload, time_t tleft,
free(ptr); free(ptr);
return (-1); return (-1);
} }
if (htons(t->th_block) != (u_short) d->xid) { if (htons(t->th_block) != (u_short)d->xid) {
/* /*
* Packet from the future, drop this. * Packet from the future, drop this.
*/ */
@ -225,7 +224,7 @@ recvtftp(struct iodesc *d, void **pkt, void **payload, time_t tleft,
* First data packet from new port. * First data packet from new port.
*/ */
struct udphdr *uh; struct udphdr *uh;
uh = (struct udphdr *) t - 1; uh = (struct udphdr *)t - 1;
d->destport = uh->uh_sport; d->destport = uh->uh_sport;
} }
got = len - (t->th_data - (char *)t); got = len - (t->th_data - (char *)t);
@ -234,7 +233,7 @@ recvtftp(struct iodesc *d, void **pkt, void **payload, time_t tleft,
return (got); return (got);
} }
case ERROR: case ERROR:
if ((unsigned) ntohs(t->th_code) > TFTP_MAX_ERRCODE) { if ((unsigned)ntohs(t->th_code) > TFTP_MAX_ERRCODE) {
printf("illegal tftp error %d\n", ntohs(t->th_code)); printf("illegal tftp error %d\n", ntohs(t->th_code));
errno = EIO; errno = EIO;
} else { } else {
@ -249,8 +248,8 @@ recvtftp(struct iodesc *d, void **pkt, void **payload, time_t tleft,
struct udphdr *uh; struct udphdr *uh;
int tftp_oack_len; int tftp_oack_len;
/* /*
* Unexpected OACK. TFTP transfer already in progress. * Unexpected OACK. TFTP transfer already in progress.
* Drop the pkt. * Drop the pkt.
*/ */
if (d->xid != 1) { if (d->xid != 1) {
@ -262,9 +261,9 @@ recvtftp(struct iodesc *d, void **pkt, void **payload, time_t tleft,
* Remember which port this OACK came from, because we need * Remember which port this OACK came from, because we need
* to send the ACK or errors back to it. * to send the ACK or errors back to it.
*/ */
uh = (struct udphdr *) t - 1; uh = (struct udphdr *)t - 1;
d->destport = uh->uh_sport; d->destport = uh->uh_sport;
/* Parse options ACK-ed by the server. */ /* Parse options ACK-ed by the server. */
tftp_oack_len = len - sizeof(t->th_opcode); tftp_oack_len = len - sizeof(t->th_opcode);
if (tftp_parse_oack(h, t->th_u.tu_stuff, tftp_oack_len) != 0) { if (tftp_parse_oack(h, t->th_u.tu_stuff, tftp_oack_len) != 0) {
@ -296,9 +295,9 @@ tftp_makereq(struct tftp_handle *h)
u_char space[FNAME_SIZE + 6]; u_char space[FNAME_SIZE + 6];
} __packed __aligned(4) wbuf; } __packed __aligned(4) wbuf;
struct tftprecv_extra recv_extra; struct tftprecv_extra recv_extra;
char *wtail; char *wtail;
int l; int l;
ssize_t res; ssize_t res;
void *pkt; void *pkt;
struct tftphdr *t; struct tftphdr *t;
char *tftp_blksize = NULL; char *tftp_blksize = NULL;
@ -312,7 +311,7 @@ tftp_makereq(struct tftp_handle *h)
tftp_set_blksize(h, tftp_blksize); tftp_set_blksize(h, tftp_blksize);
} }
wbuf.t.th_opcode = htons((u_short) RRQ); wbuf.t.th_opcode = htons((u_short)RRQ);
wtail = wbuf.t.th_stuff; wtail = wbuf.t.th_stuff;
l = strlen(h->path); l = strlen(h->path);
#ifdef TFTP_PREPEND_PATH #ifdef TFTP_PREPEND_PATH
@ -337,7 +336,6 @@ tftp_makereq(struct tftp_handle *h)
bcopy("0", wtail, 2); bcopy("0", wtail, 2);
wtail += 2; wtail += 2;
/* h->iodesc->myport = htons(--tftpport); */
h->iodesc->myport = htons(tftpport + (getsecs() & 0x3ff)); h->iodesc->myport = htons(tftpport + (getsecs() & 0x3ff));
h->iodesc->destport = htons(IPPORT_TFTP); h->iodesc->destport = htons(IPPORT_TFTP);
h->iodesc->xid = 1; /* expected block */ h->iodesc->xid = 1; /* expected block */
@ -348,8 +346,8 @@ tftp_makereq(struct tftp_handle *h)
pkt = NULL; pkt = NULL;
recv_extra.tftp_handle = h; recv_extra.tftp_handle = h;
res = sendrecv(h->iodesc, &sendudp, &wbuf.t, wtail - (char *) &wbuf.t, res = sendrecv(h->iodesc, &sendudp, &wbuf.t, wtail - (char *)&wbuf.t,
(void *)&recvtftp, &pkt, (void **)&t, &recv_extra); &recvtftp, &pkt, (void **)&t, &recv_extra);
if (res == -1) { if (res == -1) {
free(pkt); free(pkt);
return (errno); return (errno);
@ -384,7 +382,7 @@ tftp_makereq(struct tftp_handle *h)
} }
/* ack block, expect next */ /* ack block, expect next */
static int static int
tftp_getnextblock(struct tftp_handle *h) tftp_getnextblock(struct tftp_handle *h)
{ {
struct { struct {
@ -392,21 +390,22 @@ tftp_getnextblock(struct tftp_handle *h)
struct tftphdr t; struct tftphdr t;
} __packed __aligned(4) wbuf; } __packed __aligned(4) wbuf;
struct tftprecv_extra recv_extra; struct tftprecv_extra recv_extra;
char *wtail; char *wtail;
int res; int res;
void *pkt; void *pkt;
struct tftphdr *t; struct tftphdr *t;
wbuf.t.th_opcode = htons((u_short) ACK);
wtail = (char *) &wbuf.t.th_block; wbuf.t.th_opcode = htons((u_short)ACK);
wbuf.t.th_block = htons((u_short) h->currblock); wtail = (char *)&wbuf.t.th_block;
wbuf.t.th_block = htons((u_short)h->currblock);
wtail += 2; wtail += 2;
h->iodesc->xid = h->currblock + 1; /* expected block */ h->iodesc->xid = h->currblock + 1; /* expected block */
pkt = NULL; pkt = NULL;
recv_extra.tftp_handle = h; recv_extra.tftp_handle = h;
res = sendrecv(h->iodesc, &sendudp, &wbuf.t, wtail - (char *) &wbuf.t, res = sendrecv(h->iodesc, &sendudp, &wbuf.t, wtail - (char *)&wbuf.t,
(void *)&recvtftp, &pkt, (void **)&t, &recv_extra); &recvtftp, &pkt, (void **)&t, &recv_extra);
if (res == -1) { /* 0 is OK! */ if (res == -1) { /* 0 is OK! */
free(pkt); free(pkt);
@ -422,8 +421,8 @@ tftp_getnextblock(struct tftp_handle *h)
h->islastblock = 1; /* EOF */ h->islastblock = 1; /* EOF */
if (h->islastblock == 1) { if (h->islastblock == 1) {
/* Send an ACK for the last block */ /* Send an ACK for the last block */
wbuf.t.th_block = htons((u_short) h->currblock); wbuf.t.th_block = htons((u_short)h->currblock);
sendudp(h->iodesc, &wbuf.t, wtail - (char *)&wbuf.t); sendudp(h->iodesc, &wbuf.t, wtail - (char *)&wbuf.t);
} }
@ -434,10 +433,10 @@ static int
tftp_open(const char *path, struct open_file *f) tftp_open(const char *path, struct open_file *f)
{ {
struct tftp_handle *tftpfile; struct tftp_handle *tftpfile;
struct iodesc *io; struct iodesc *io;
int res; int res;
size_t pathsize; size_t pathsize;
const char *extraslash; const char *extraslash;
if (netproto != NET_TFTP) if (netproto != NET_TFTP)
return (EINVAL); return (EINVAL);
@ -453,7 +452,7 @@ tftp_open(const char *path, struct open_file *f)
return (ENOMEM); return (ENOMEM);
tftpfile->tftp_blksize = TFTP_REQUESTED_BLKSIZE; tftpfile->tftp_blksize = TFTP_REQUESTED_BLKSIZE;
tftpfile->iodesc = io = socktodesc(*(int *) (f->f_devdata)); tftpfile->iodesc = io = socktodesc(*(int *)(f->f_devdata));
if (io == NULL) { if (io == NULL) {
free(tftpfile); free(tftpfile);
return (EINVAL); return (EINVAL);
@ -465,7 +464,7 @@ tftp_open(const char *path, struct open_file *f)
tftpfile->path = malloc(pathsize); tftpfile->path = malloc(pathsize);
if (tftpfile->path == NULL) { if (tftpfile->path == NULL) {
free(tftpfile); free(tftpfile);
return(ENOMEM); return (ENOMEM);
} }
if (rootpath[strlen(rootpath) - 1] == '/' || path[0] == '/') if (rootpath[strlen(rootpath) - 1] == '/' || path[0] == '/')
extraslash = ""; extraslash = "";
@ -476,7 +475,7 @@ tftp_open(const char *path, struct open_file *f)
if (res < 0 || res > pathsize) { if (res < 0 || res > pathsize) {
free(tftpfile->path); free(tftpfile->path);
free(tftpfile); free(tftpfile);
return(ENOMEM); return (ENOMEM);
} }
res = tftp_makereq(tftpfile); res = tftp_makereq(tftpfile);
@ -487,7 +486,7 @@ tftp_open(const char *path, struct open_file *f)
free(tftpfile); free(tftpfile);
return (res); return (res);
} }
f->f_fsdata = (void *) tftpfile; f->f_fsdata = tftpfile;
is_open = 1; is_open = 1;
return (0); return (0);
} }
@ -502,7 +501,7 @@ tftp_read(struct open_file *f, void *addr, size_t size,
rc = 0; rc = 0;
res = size; res = size;
tftpfile = (struct tftp_handle *) f->f_fsdata; tftpfile = f->f_fsdata;
/* Make sure we will not read past file end */ /* Make sure we will not read past file end */
if (tftpfile->tftp_tsize > 0 && if (tftpfile->tftp_tsize > 0 &&
@ -575,11 +574,11 @@ tftp_read(struct open_file *f, void *addr, size_t size,
return (rc); return (rc);
} }
static int static int
tftp_close(struct open_file *f) tftp_close(struct open_file *f)
{ {
struct tftp_handle *tftpfile; struct tftp_handle *tftpfile;
tftpfile = (struct tftp_handle *) f->f_fsdata; tftpfile = f->f_fsdata;
/* let it time out ... */ /* let it time out ... */
@ -592,17 +591,17 @@ tftp_close(struct open_file *f)
return (0); return (0);
} }
static int static int
tftp_stat(struct open_file *f, struct stat *sb) tftp_stat(struct open_file *f, struct stat *sb)
{ {
struct tftp_handle *tftpfile; struct tftp_handle *tftpfile;
tftpfile = (struct tftp_handle *) f->f_fsdata; tftpfile = f->f_fsdata;
sb->st_mode = 0444 | S_IFREG; sb->st_mode = 0444 | S_IFREG;
sb->st_nlink = 1; sb->st_nlink = 1;
sb->st_uid = 0; sb->st_uid = 0;
sb->st_gid = 0; sb->st_gid = 0;
sb->st_size = (off_t) tftpfile->tftp_tsize; sb->st_size = tftpfile->tftp_tsize;
return (0); return (0);
} }
@ -610,7 +609,7 @@ static off_t
tftp_seek(struct open_file *f, off_t offset, int where) tftp_seek(struct open_file *f, off_t offset, int where)
{ {
struct tftp_handle *tftpfile; struct tftp_handle *tftpfile;
tftpfile = (struct tftp_handle *) f->f_fsdata; tftpfile = f->f_fsdata;
switch (where) { switch (where) {
case SEEK_SET: case SEEK_SET:
@ -629,7 +628,7 @@ tftp_seek(struct open_file *f, off_t offset, int where)
static int static int
tftp_set_blksize(struct tftp_handle *h, const char *str) tftp_set_blksize(struct tftp_handle *h, const char *str)
{ {
char *endptr; char *endptr;
int new_blksize; int new_blksize;
int ret = 0; int ret = 0;
@ -644,8 +643,8 @@ tftp_set_blksize(struct tftp_handle *h, const char *str)
* RFC2348 specifies that acceptable values are 8-65464. * RFC2348 specifies that acceptable values are 8-65464.
* Let's choose a limit less than MAXRSPACE. * Let's choose a limit less than MAXRSPACE.
*/ */
if (*endptr == '\0' && new_blksize >= 8 if (*endptr == '\0' && new_blksize >= 8 &&
&& new_blksize <= TFTP_MAX_BLKSIZE) { new_blksize <= TFTP_MAX_BLKSIZE) {
h->tftp_blksize = new_blksize; h->tftp_blksize = new_blksize;
ret = 1; ret = 1;
} }
@ -676,10 +675,10 @@ tftp_set_blksize(struct tftp_handle *h, const char *str)
* optN, valueN * optN, valueN
* The final option/value acknowledgment pair. * The final option/value acknowledgment pair.
*/ */
static int static int
tftp_parse_oack(struct tftp_handle *h, char *buf, size_t len) tftp_parse_oack(struct tftp_handle *h, char *buf, size_t len)
{ {
/* /*
* We parse the OACK strings into an array * We parse the OACK strings into an array
* of name-value pairs. * of name-value pairs.
*/ */
@ -689,7 +688,7 @@ tftp_parse_oack(struct tftp_handle *h, char *buf, size_t len)
int option_idx = 0; int option_idx = 0;
int blksize_is_set = 0; int blksize_is_set = 0;
int tsize = 0; int tsize = 0;
unsigned int orig_blksize; unsigned int orig_blksize;
while (option_idx < 128 && i < len) { while (option_idx < 128 && i < len) {
@ -706,26 +705,30 @@ tftp_parse_oack(struct tftp_handle *h, char *buf, size_t len)
/* Save the block size we requested for sanity check later. */ /* Save the block size we requested for sanity check later. */
orig_blksize = h->tftp_blksize; orig_blksize = h->tftp_blksize;
/* /*
* Parse individual TFTP options. * Parse individual TFTP options.
* * "blksize" is specified in RFC2348. * * "blksize" is specified in RFC2348.
* * "tsize" is specified in RFC2349. * * "tsize" is specified in RFC2349.
*/ */
for (i = 0; i < option_idx; i += 2) { for (i = 0; i < option_idx; i += 2) {
if (strcasecmp(tftp_options[i], "blksize") == 0) { if (strcasecmp(tftp_options[i], "blksize") == 0) {
if (i + 1 < option_idx) if (i + 1 < option_idx)
blksize_is_set = blksize_is_set =
tftp_set_blksize(h, tftp_options[i + 1]); tftp_set_blksize(h, tftp_options[i + 1]);
} else if (strcasecmp(tftp_options[i], "tsize") == 0) { } else if (strcasecmp(tftp_options[i], "tsize") == 0) {
if (i + 1 < option_idx) if (i + 1 < option_idx)
tsize = strtol(tftp_options[i + 1], (char **)NULL, 10); tsize = strtol(tftp_options[i + 1], NULL, 10);
if (tsize != 0) if (tsize != 0)
h->tftp_tsize = tsize; h->tftp_tsize = tsize;
} else { } else {
/* Do not allow any options we did not expect to be ACKed. */ /*
printf("unexpected tftp option '%s'\n", tftp_options[i]); * Do not allow any options we did not expect to be
return (-1); * ACKed.
} */
printf("unexpected tftp option '%s'\n",
tftp_options[i]);
return (-1);
}
} }
if (!blksize_is_set) { if (!blksize_is_set) {
@ -747,5 +750,5 @@ tftp_parse_oack(struct tftp_handle *h, char *buf, size_t len)
printf("tftp_blksize: %u\n", h->tftp_blksize); printf("tftp_blksize: %u\n", h->tftp_blksize);
printf("tftp_tsize: %lu\n", h->tftp_tsize); printf("tftp_tsize: %lu\n", h->tftp_tsize);
#endif #endif
return 0; return (0);
} }