summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorToomas Soome <tsoome@me.com>2018-09-04 16:30:41 +0300
committerRobert Mustacchi <rm@joyent.com>2018-09-21 22:57:36 +0000
commit921f410897840ae3150db7a9e99cb22126a52ad1 (patch)
treee29e8326a4ad0228acae058b8ce068a4de7b63bc
parent4667a9b13a267e53bf6c785cb5745b689f413f1d (diff)
downloadillumos-joyent-921f410897840ae3150db7a9e99cb22126a52ad1.tar.gz
9799 libstand: tftp.c cstyle cleanup
Reviewed by: Jason King <jason.king@joyent.com> Reviewed by: Andy Fiddaman <omnios@citrus-it.co.uk> Reviewed by: Igor Kozhukhov <igor@dilos.org> Reviewed by: Gergő Mihály Doma <domag02@gmail.com> Approved by: Robert Mustacchi <rm@joyent.com>
-rw-r--r--usr/src/boot/lib/libstand/tftp.c232
1 files changed, 117 insertions, 115 deletions
diff --git a/usr/src/boot/lib/libstand/tftp.c b/usr/src/boot/lib/libstand/tftp.c
index 1e6beb0378..503626d89b 100644
--- a/usr/src/boot/lib/libstand/tftp.c
+++ b/usr/src/boot/lib/libstand/tftp.c
@@ -62,30 +62,29 @@
struct tftp_handle;
struct tftprecv_extra;
-static ssize_t recvtftp(struct iodesc *d, void **pkt, void **payload,
- time_t tleft, void *recv_extra);
-static int tftp_open(const char *path, struct open_file *f);
-static int tftp_close(struct open_file *f);
-static int tftp_parse_oack(struct tftp_handle *h, char *buf, size_t len);
-static int tftp_read(struct open_file *f, void *buf, size_t size, size_t *resid);
-static off_t tftp_seek(struct open_file *f, off_t offset, int where);
-static int tftp_set_blksize(struct tftp_handle *h, const char *str);
-static int tftp_stat(struct open_file *f, struct stat *sb);
+static ssize_t recvtftp(struct iodesc *, void **, void **, time_t, void *);
+static int tftp_open(const char *, struct open_file *);
+static int tftp_close(struct open_file *);
+static int tftp_parse_oack(struct tftp_handle *, char *, size_t);
+static int tftp_read(struct open_file *, void *, size_t, size_t *);
+static off_t tftp_seek(struct open_file *, off_t, int);
+static int tftp_set_blksize(struct tftp_handle *, const char *);
+static int tftp_stat(struct open_file *, struct stat *);
struct fs_ops tftp_fsops = {
- "tftp",
- tftp_open,
- tftp_close,
- tftp_read,
- null_write,
- tftp_seek,
- tftp_stat,
- null_readdir
+ .fs_name = "tftp",
+ .fo_open = tftp_open,
+ .fo_close = tftp_close,
+ .fo_read = tftp_read,
+ .fo_write = null_write,
+ .fo_seek = tftp_seek,
+ .fo_stat = tftp_stat,
+ .fo_readdir = null_readdir
};
extern struct in_addr servip;
-static int tftpport = 2000;
+static int tftpport = 2000;
static int is_open = 0;
/*
@@ -93,21 +92,21 @@ static int is_open = 0;
* TFTP_REQUESTED_BLKSIZE of 1428 is (Ethernet MTU, less the TFTP, UDP and
* IP header lengths).
*/
-#define TFTP_REQUESTED_BLKSIZE 1428
+#define TFTP_REQUESTED_BLKSIZE 1428
/*
* Choose a blksize big enough so we can test with Ethernet
* Jumbo frames in the future.
*/
-#define TFTP_MAX_BLKSIZE 9008
+#define TFTP_MAX_BLKSIZE 9008
struct tftp_handle {
struct iodesc *iodesc;
- int currblock; /* contents of lastdata */
- int islastblock; /* flag */
- int validsize;
- int off;
- char *path; /* saved for re-requests */
+ int currblock; /* contents of lastdata */
+ int islastblock; /* flag */
+ int validsize;
+ int off;
+ char *path; /* saved for re-requests */
unsigned int tftp_blksize;
unsigned long tftp_tsize;
void *pkt;
@@ -136,46 +135,46 @@ static int tftp_getnextblock(struct tftp_handle *h);
/* send error message back. */
static void
-tftp_senderr(struct tftp_handle *h, u_short errcode, const char *msg)
+tftp_senderr(struct tftp_handle *h, ushort_t errcode, const char *msg)
{
struct {
- u_char header[HEADER_SIZE];
- struct tftphdr t;
- u_char space[63]; /* +1 from t */
+ uchar_t header[HEADER_SIZE];
+ struct tftphdr t;
+ uchar_t space[63]; /* +1 from t */
} __packed __aligned(4) wbuf;
- char *wtail;
- int len;
+ char *wtail;
+ int len;
len = strlen(msg);
- if (len > sizeof(wbuf.space))
- len = sizeof(wbuf.space);
+ if (len > sizeof (wbuf.space))
+ len = sizeof (wbuf.space);
- wbuf.t.th_opcode = htons((u_short) ERROR);
- wbuf.t.th_code = htons(errcode);
+ wbuf.t.th_opcode = htons((ushort_t)ERROR);
+ wbuf.t.th_code = htons(errcode);
wtail = wbuf.t.th_msg;
bcopy(msg, wtail, len);
wtail[len] = '\0';
wtail += len + 1;
- sendudp(h->iodesc, &wbuf.t, wtail - (char *) &wbuf.t);
+ sendudp(h->iodesc, &wbuf.t, wtail - (char *)&wbuf.t);
}
static void
tftp_sendack(struct tftp_handle *h, ushort_t block)
{
struct {
- u_char header[HEADER_SIZE];
+ uchar_t header[HEADER_SIZE];
struct tftphdr t;
} __packed __aligned(4) wbuf;
- char *wtail;
+ char *wtail;
- wbuf.t.th_opcode = htons((u_short) ACK);
- wtail = (char *) &wbuf.t.th_block;
+ wbuf.t.th_opcode = htons((ushort_t)ACK);
+ wtail = (char *)&wbuf.t.th_block;
wbuf.t.th_block = htons(block);
wtail += 2;
- sendudp(h->iodesc, &wbuf.t, wtail - (char *) &wbuf.t);
+ sendudp(h->iodesc, &wbuf.t, wtail - (char *)&wbuf.t);
}
static ssize_t
@@ -189,7 +188,7 @@ recvtftp(struct iodesc *d, void **pkt, void **payload, time_t tleft,
ssize_t len;
errno = 0;
- extra = (struct tftprecv_extra *)recv_extra;
+ extra = recv_extra;
h = extra->tftp_handle;
len = readudp(d, &ptr, (void **)&t, tleft);
@@ -204,7 +203,7 @@ recvtftp(struct iodesc *d, void **pkt, void **payload, time_t tleft,
case DATA: {
int got;
- if (htons(t->th_block) < (ushort_t) d->xid) {
+ if (htons(t->th_block) < (ushort_t)d->xid) {
/*
* Apparently our ACK was missed, re-send.
*/
@@ -212,7 +211,7 @@ recvtftp(struct iodesc *d, void **pkt, void **payload, time_t tleft,
free(ptr);
return (-1);
}
- if (htons(t->th_block) != (u_short) d->xid) {
+ if (htons(t->th_block) != (ushort_t)d->xid) {
/*
* Packet from the future, drop this.
*/
@@ -224,7 +223,7 @@ recvtftp(struct iodesc *d, void **pkt, void **payload, time_t tleft,
* First data packet from new port.
*/
struct udphdr *uh;
- uh = (struct udphdr *) t - 1;
+ uh = (struct udphdr *)t - 1;
d->destport = uh->uh_sport;
}
got = len - (t->th_data - (char *)t);
@@ -233,7 +232,7 @@ recvtftp(struct iodesc *d, void **pkt, void **payload, time_t tleft,
return (got);
}
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));
errno = EIO;
} else {
@@ -248,8 +247,8 @@ recvtftp(struct iodesc *d, void **pkt, void **payload, time_t tleft,
struct udphdr *uh;
int tftp_oack_len;
- /*
- * Unexpected OACK. TFTP transfer already in progress.
+ /*
+ * Unexpected OACK. TFTP transfer already in progress.
* Drop the pkt.
*/
if (d->xid != 1) {
@@ -261,11 +260,11 @@ recvtftp(struct iodesc *d, void **pkt, void **payload, time_t tleft,
* Remember which port this OACK came from, because we need
* to send the ACK or errors back to it.
*/
- uh = (struct udphdr *) t - 1;
+ uh = (struct udphdr *)t - 1;
d->destport = uh->uh_sport;
-
+
/* 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) {
tftp_senderr(h, EOPTNEG, "Malformed OACK");
errno = EIO;
@@ -290,14 +289,14 @@ static int
tftp_makereq(struct tftp_handle *h)
{
struct {
- u_char header[HEADER_SIZE];
+ uchar_t header[HEADER_SIZE];
struct tftphdr t;
- u_char space[FNAME_SIZE + 6];
+ uchar_t space[FNAME_SIZE + 6];
} __packed __aligned(4) wbuf;
struct tftprecv_extra recv_extra;
- char *wtail;
- int l;
- ssize_t res;
+ char *wtail;
+ int l;
+ ssize_t res;
void *pkt;
struct tftphdr *t;
char *tftp_blksize = NULL;
@@ -311,14 +310,14 @@ tftp_makereq(struct tftp_handle *h)
tftp_set_blksize(h, tftp_blksize);
}
- wbuf.t.th_opcode = htons((u_short) RRQ);
+ wbuf.t.th_opcode = htons((ushort_t)RRQ);
wtail = wbuf.t.th_stuff;
l = strlen(h->path);
#ifdef TFTP_PREPEND_PATH
- if (l > FNAME_SIZE - (sizeof(TFTP_PREPEND_PATH) - 1))
+ if (l > FNAME_SIZE - (sizeof (TFTP_PREPEND_PATH) - 1))
return (ENAMETOOLONG);
- bcopy(TFTP_PREPEND_PATH, wtail, sizeof(TFTP_PREPEND_PATH) - 1);
- wtail += sizeof(TFTP_PREPEND_PATH) - 1;
+ bcopy(TFTP_PREPEND_PATH, wtail, sizeof (TFTP_PREPEND_PATH) - 1);
+ wtail += sizeof (TFTP_PREPEND_PATH) - 1;
#else
if (l > FNAME_SIZE)
return (ENAMETOOLONG);
@@ -336,7 +335,6 @@ tftp_makereq(struct tftp_handle *h)
bcopy("0", wtail, 2);
wtail += 2;
- /* h->iodesc->myport = htons(--tftpport); */
h->iodesc->myport = htons(tftpport + (getsecs() & 0x3ff));
h->iodesc->destport = htons(IPPORT_TFTP);
h->iodesc->xid = 1; /* expected block */
@@ -347,8 +345,8 @@ tftp_makereq(struct tftp_handle *h)
pkt = NULL;
recv_extra.tftp_handle = h;
- res = sendrecv(h->iodesc, &sendudp, &wbuf.t, wtail - (char *) &wbuf.t,
- &recvtftp, &pkt, (void **)&t, &recv_extra);
+ res = sendrecv(h->iodesc, &sendudp, &wbuf.t, wtail - (char *)&wbuf.t,
+ &recvtftp, &pkt, (void **)&t, &recv_extra);
if (res == -1) {
free(pkt);
return (errno);
@@ -383,29 +381,30 @@ tftp_makereq(struct tftp_handle *h)
}
/* ack block, expect next */
-static int
+static int
tftp_getnextblock(struct tftp_handle *h)
{
struct {
- u_char header[HEADER_SIZE];
+ uchar_t header[HEADER_SIZE];
struct tftphdr t;
} __packed __aligned(4) wbuf;
struct tftprecv_extra recv_extra;
- char *wtail;
- int res;
+ char *wtail;
+ int res;
void *pkt;
struct tftphdr *t;
- wbuf.t.th_opcode = htons((u_short) ACK);
- wtail = (char *) &wbuf.t.th_block;
- wbuf.t.th_block = htons((u_short) h->currblock);
+
+ wbuf.t.th_opcode = htons((ushort_t)ACK);
+ wtail = (char *)&wbuf.t.th_block;
+ wbuf.t.th_block = htons((ushort_t)h->currblock);
wtail += 2;
h->iodesc->xid = h->currblock + 1; /* expected block */
pkt = NULL;
recv_extra.tftp_handle = h;
- res = sendrecv(h->iodesc, &sendudp, &wbuf.t, wtail - (char *) &wbuf.t,
- &recvtftp, &pkt, (void **)&t, &recv_extra);
+ res = sendrecv(h->iodesc, &sendudp, &wbuf.t, wtail - (char *)&wbuf.t,
+ &recvtftp, &pkt, (void **)&t, &recv_extra);
if (res == -1) { /* 0 is OK! */
free(pkt);
@@ -421,8 +420,8 @@ tftp_getnextblock(struct tftp_handle *h)
h->islastblock = 1; /* EOF */
if (h->islastblock == 1) {
- /* Send an ACK for the last block */
- wbuf.t.th_block = htons((u_short) h->currblock);
+ /* Send an ACK for the last block */
+ wbuf.t.th_block = htons((ushort_t)h->currblock);
sendudp(h->iodesc, &wbuf.t, wtail - (char *)&wbuf.t);
}
@@ -433,10 +432,10 @@ static int
tftp_open(const char *path, struct open_file *f)
{
struct tftp_handle *tftpfile;
- struct iodesc *io;
- int res;
- size_t pathsize;
- const char *extraslash;
+ struct iodesc *io;
+ int res;
+ size_t pathsize;
+ const char *extraslash;
if (netproto != NET_TFTP)
return (EINVAL);
@@ -447,13 +446,12 @@ tftp_open(const char *path, struct open_file *f)
if (is_open)
return (EBUSY);
- tftpfile = (struct tftp_handle *) malloc(sizeof(*tftpfile));
+ tftpfile = calloc(1, sizeof (*tftpfile));
if (!tftpfile)
return (ENOMEM);
- memset(tftpfile, 0, sizeof(*tftpfile));
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) {
free(tftpfile);
return (EINVAL);
@@ -461,11 +459,11 @@ tftp_open(const char *path, struct open_file *f)
io->destip = servip;
tftpfile->off = 0;
- pathsize = (strlen(rootpath) + 1 + strlen(path) + 1) * sizeof(char);
+ pathsize = (strlen(rootpath) + 1 + strlen(path) + 1) * sizeof (char);
tftpfile->path = malloc(pathsize);
if (tftpfile->path == NULL) {
free(tftpfile);
- return(ENOMEM);
+ return (ENOMEM);
}
if (rootpath[strlen(rootpath) - 1] == '/' || path[0] == '/')
extraslash = "";
@@ -476,7 +474,7 @@ tftp_open(const char *path, struct open_file *f)
if (res < 0 || res > pathsize) {
free(tftpfile->path);
free(tftpfile);
- return(ENOMEM);
+ return (ENOMEM);
}
res = tftp_makereq(tftpfile);
@@ -487,7 +485,7 @@ tftp_open(const char *path, struct open_file *f)
free(tftpfile);
return (res);
}
- f->f_fsdata = (void *) tftpfile;
+ f->f_fsdata = tftpfile;
is_open = 1;
return (0);
}
@@ -502,7 +500,7 @@ tftp_read(struct open_file *f, void *addr, size_t size,
rc = 0;
res = size;
- tftpfile = (struct tftp_handle *) f->f_fsdata;
+ tftpfile = f->f_fsdata;
/* Make sure we will not read past file end */
if (tftpfile->tftp_tsize > 0 &&
@@ -575,11 +573,11 @@ tftp_read(struct open_file *f, void *addr, size_t size,
return (rc);
}
-static int
+static int
tftp_close(struct open_file *f)
{
struct tftp_handle *tftpfile;
- tftpfile = (struct tftp_handle *) f->f_fsdata;
+ tftpfile = f->f_fsdata;
/* let it time out ... */
@@ -592,17 +590,17 @@ tftp_close(struct open_file *f)
return (0);
}
-static int
+static int
tftp_stat(struct open_file *f, struct stat *sb)
{
struct tftp_handle *tftpfile;
- tftpfile = (struct tftp_handle *) f->f_fsdata;
+ tftpfile = f->f_fsdata;
sb->st_mode = 0444 | S_IFREG;
sb->st_nlink = 1;
sb->st_uid = 0;
sb->st_gid = 0;
- sb->st_size = (off_t) tftpfile->tftp_tsize;
+ sb->st_size = tftpfile->tftp_tsize;
return (0);
}
@@ -610,7 +608,7 @@ static off_t
tftp_seek(struct open_file *f, off_t offset, int where)
{
struct tftp_handle *tftpfile;
- tftpfile = (struct tftp_handle *) f->f_fsdata;
+ tftpfile = f->f_fsdata;
switch (where) {
case SEEK_SET:
@@ -629,7 +627,7 @@ tftp_seek(struct open_file *f, off_t offset, int where)
static int
tftp_set_blksize(struct tftp_handle *h, const char *str)
{
- char *endptr;
+ char *endptr;
int new_blksize;
int ret = 0;
@@ -644,8 +642,8 @@ tftp_set_blksize(struct tftp_handle *h, const char *str)
* RFC2348 specifies that acceptable values are 8-65464.
* Let's choose a limit less than MAXRSPACE.
*/
- if (*endptr == '\0' && new_blksize >= 8
- && new_blksize <= TFTP_MAX_BLKSIZE) {
+ if (*endptr == '\0' && new_blksize >= 8 &&
+ new_blksize <= TFTP_MAX_BLKSIZE) {
h->tftp_blksize = new_blksize;
ret = 1;
}
@@ -676,10 +674,10 @@ tftp_set_blksize(struct tftp_handle *h, const char *str)
* optN, valueN
* The final option/value acknowledgment pair.
*/
-static int
+static int
tftp_parse_oack(struct tftp_handle *h, char *buf, size_t len)
{
- /*
+ /*
* We parse the OACK strings into an array
* of name-value pairs.
*/
@@ -689,7 +687,7 @@ tftp_parse_oack(struct tftp_handle *h, char *buf, size_t len)
int option_idx = 0;
int blksize_is_set = 0;
int tsize = 0;
-
+
unsigned int orig_blksize;
while (option_idx < 128 && i < len) {
@@ -706,26 +704,30 @@ tftp_parse_oack(struct tftp_handle *h, char *buf, size_t len)
/* Save the block size we requested for sanity check later. */
orig_blksize = h->tftp_blksize;
- /*
+ /*
* Parse individual TFTP options.
* * "blksize" is specified in RFC2348.
* * "tsize" is specified in RFC2349.
- */
+ */
for (i = 0; i < option_idx; i += 2) {
- if (strcasecmp(tftp_options[i], "blksize") == 0) {
- if (i + 1 < option_idx)
- blksize_is_set =
- tftp_set_blksize(h, tftp_options[i + 1]);
- } else if (strcasecmp(tftp_options[i], "tsize") == 0) {
- if (i + 1 < option_idx)
- tsize = strtol(tftp_options[i + 1], (char **)NULL, 10);
- if (tsize != 0)
- h->tftp_tsize = tsize;
- } else {
- /* Do not allow any options we did not expect to be ACKed. */
- printf("unexpected tftp option '%s'\n", tftp_options[i]);
- return (-1);
- }
+ if (strcasecmp(tftp_options[i], "blksize") == 0) {
+ if (i + 1 < option_idx)
+ blksize_is_set =
+ tftp_set_blksize(h, tftp_options[i + 1]);
+ } else if (strcasecmp(tftp_options[i], "tsize") == 0) {
+ if (i + 1 < option_idx)
+ tsize = strtol(tftp_options[i + 1], NULL, 10);
+ if (tsize != 0)
+ h->tftp_tsize = tsize;
+ } else {
+ /*
+ * Do not allow any options we did not expect to be
+ * ACKed.
+ */
+ printf("unexpected tftp option '%s'\n",
+ tftp_options[i]);
+ return (-1);
+ }
}
if (!blksize_is_set) {
@@ -747,5 +749,5 @@ tftp_parse_oack(struct tftp_handle *h, char *buf, size_t len)
printf("tftp_blksize: %u\n", h->tftp_blksize);
printf("tftp_tsize: %lu\n", h->tftp_tsize);
#endif
- return 0;
+ return (0);
}