From 903ad05d18848488aa0c47f39e623e747d3370ed Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sat, 31 Oct 2026 17:33:26 -0500 Subject: [PATCH] Cleanup. --- toys/pending/tftp.c | 91 +++++++++++++++++++++------------------------ 1 file changed, 43 insertions(+), 48 deletions(-) diff --git a/toys/pending/tftp.c b/toys/pending/tftp.c index 41308936..299f2538 100644 --- a/toys/pending/tftp.c +++ b/toys/pending/tftp.c @@ -5,7 +5,7 @@ * * No Standard. -USE_TFTP(NEWTOY(tftp, "<1b#<8>65464r:l:g|p|[!gp]", TOYFLAG_USR|TOYFLAG_BIN)) +USE_TFTP(NEWTOY(tftp, "<1b#<8>65464R#<0=3r:l:g|p|[!gp]", TOYFLAG_USR|TOYFLAG_BIN)) config TFTP bool "tftp" @@ -17,6 +17,7 @@ config TFTP -l FILE Local FILE -r FILE Remote FILE + -R Retries (default 3) -g Get file -p Put file -b SIZE Transfer blocks of SIZE octets(8 <= SIZE <= 65464) @@ -26,14 +27,13 @@ config TFTP GLOBALS( char *l, *r; - long block_size; + long R, block_size; struct sockaddr_storage inaddr; int af; ) #define TFTP_BLKSIZE 512 -#define TFTP_RETRIES 3 #define TFTP_DATAHEADERSIZE 4 #define TFTP_MAXPACKETSIZE (TFTP_DATAHEADERSIZE + TFTP_BLKSIZE) #define TFTP_PACKETSIZE TFTP_MAXPACKETSIZE @@ -62,16 +62,14 @@ GLOBALS( // Initializes SERVER with ADDR and returns socket. static int init_tftp(struct sockaddr_storage *server) { - struct timeval to = { .tv_sec = 10, //Time out - .tv_usec = 0 }; - const int set = 1; - int port = 69, sd = xsocket(TT.af, SOCK_DGRAM, IPPROTO_UDP); + struct timeval to = {.tv_sec = 10}; + int set = 1, port = 69, sd = xsocket(TT.af, SOCK_DGRAM, IPPROTO_UDP); - xsetsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, (void *)&to, sizeof(struct timeval)); - xsetsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (void *)&set, sizeof(set)); + xsetsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof(struct timeval)); + xsetsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &set, sizeof(set)); if(toys.optc == 2) port = atolx_range(toys.optargs[1], 1, 65535); - memset(server, 0, sizeof(struct sockaddr_storage)); + memset(server, 0, sizeof(*server)); if (TT.af == AF_INET6) { ((struct sockaddr_in6 *)server)->sin6_family = AF_INET6; ((struct sockaddr_in6 *)server)->sin6_addr = @@ -87,11 +85,9 @@ static int init_tftp(struct sockaddr_storage *server) return sd; } -/* - * Makes a request packet in BUFFER with OPCODE and file PATH of MODE - * and returns length of packet. - */ -static int mkpkt_request(uint8_t *buffer, int opcode, char *path, int mode) +// Makes a request packet in BUFFER with OPCODE and file PATH of MODE +// and returns length of packet. +static int mkpkt_request(char *buffer, int opcode, char *path, int mode) { buffer[0] = opcode >> 8; buffer[1] = opcode & 0xff; @@ -104,7 +100,7 @@ static int mkpkt_request(uint8_t *buffer, int opcode, char *path, int mode) * Makes an acknowledgement packet in BUFFER of BLOCNO * and returns packet length. */ -static int mkpkt_ack(uint8_t *buffer, uint16_t blockno) +static int mkpkt_ack(char *buffer, unsigned short blockno) { buffer[0] = TFTP_OP_ACK >> 8; buffer[1] = TFTP_OP_ACK & 0xff; @@ -117,7 +113,7 @@ static int mkpkt_ack(uint8_t *buffer, uint16_t blockno) * Makes an error packet in BUFFER with ERRORCODE and ERRORMSG. * and returns packet length. */ -static int mkpkt_err(uint8_t *buffer, uint16_t errorcode, char *errormsg) +static int mkpkt_err(char *buffer, unsigned short errorcode, char *errormsg) { buffer[0] = TFTP_OP_ERR >> 8; buffer[1] = TFTP_OP_ERR & 0xff; @@ -177,19 +173,19 @@ static ssize_t write_server(int sd, void *buf, size_t len, } // checks packet for data and updates block no -static inline int check_data( uint8_t *packet, uint16_t *opcode, - uint16_t *blockno) +static inline int check_data(char *packet, unsigned short *opcode, + unsigned short *blockno) { - *opcode = (uint16_t) packet[0] << 8 | (uint16_t) packet[1]; + *opcode = packet[0] << 8 | packet[1]; if (*opcode == TFTP_OP_DATA) { - *blockno = (uint16_t) packet[2] << 8 | (uint16_t) packet[3]; + *blockno = packet[2] << 8 | packet[3]; return 0; } return -1; } // Makes data packet through FD from file OFFSET in buffer PACKET of BLOCKNO -static int mkpkt_data(int fd, off_t offset, uint8_t *packet, uint16_t blockno) +static int mkpkt_data(int fd, off_t offset, char *packet, unsigned blockno) { off_t tmp; int nbytesread; @@ -209,15 +205,14 @@ static int mkpkt_data(int fd, off_t offset, uint8_t *packet, uint16_t blockno) } // Receives ACK responses from server and updates blockno -static int read_ack(int sd, uint8_t *packet, struct sockaddr_storage *server, - uint16_t *port, uint16_t *blockno) +static int read_ack(int sd, char *packet, struct sockaddr_storage *server, + unsigned short *port, unsigned short *blockno) { struct sockaddr_storage from; - int nbytes; - uint16_t opcode, rblockno; - int packetlen, retry; + unsigned short opcode, rblockno; + int packetlen, retry, nbytes; - for (retry = 0; retry < TFTP_RETRIES; retry++) { + for (retry = 0; retry < TT.R; retry++) { for (;;) { nbytes = read_server(sd, packet, TFTP_IOBUFSIZE, &from); if (nbytes < 4) { // Ack headersize = 4 @@ -239,17 +234,17 @@ static int read_ack(int sd, uint8_t *packet, struct sockaddr_storage *server, if (*port != ((struct sockaddr_in *)server)->sin_port) { error_msg("Invalid port in DATA."); packetlen = mkpkt_err(packet, TFTP_ER_UNKID, TFTP_ES_UNKID); - (void) write_server(sd, packet, packetlen, server); + write_server(sd, packet, packetlen, server); continue; } - opcode = (uint16_t) packet[0] << 8 | (uint16_t) packet[1]; - rblockno = (uint16_t) packet[2] << 8 | (uint16_t) packet[3]; + opcode = packet[0] << 8 | packet[1]; + rblockno = packet[2] << 8 | packet[3]; if (opcode != TFTP_OP_ACK) { error_msg("Bad opcode."); if (opcode > 5) { packetlen = mkpkt_err(packet, TFTP_ER_ILLEGALOP, TFTP_ES_ILLEGALOP); - (void) write_server(sd, packet, packetlen, server); + write_server(sd, packet, packetlen, server); } break; } @@ -266,13 +261,13 @@ static int read_ack(int sd, uint8_t *packet, struct sockaddr_storage *server, static int file_get(void) { struct sockaddr_storage server, from; - uint8_t *packet; - uint16_t blockno = 0, opcode, rblockno = 0; + unsigned short blockno = 0, opcode, rblockno = 0; int len, sd, fd, retry, nbytesrecvd = 0, ndatabytes, ret, result = -1; + char *packet; sd = init_tftp(&server); - packet = (uint8_t*) xzalloc(TFTP_IOBUFSIZE); + packet = xzalloc(TFTP_IOBUFSIZE); fd = xcreate(TT.l, O_WRONLY | O_CREAT | O_TRUNC, 0666); len = mkpkt_request(packet, TFTP_OP_RRQ, TT.r, 1); @@ -286,7 +281,7 @@ static int file_get(void) do { blockno++; - for (retry = 0 ; retry < TFTP_RETRIES; retry++) { + for (retry = 0 ; retry < TT.R; retry++) { nbytesrecvd = read_server(sd, packet, TFTP_IOBUFSIZE, &from); if (nbytesrecvd > 0) { if ( ((TT.af == AF_INET) && @@ -352,7 +347,7 @@ static int file_get(void) break; } } - if (retry == TFTP_RETRIES) { + if (retry == TT.R) { error_msg("Retry limit exceeded."); unlink(TT.l); goto errout_with_sd; @@ -381,13 +376,13 @@ errout_with_sd: xclose(sd); int file_put(void) { struct sockaddr_storage server; - uint8_t *packet; + char *packet; off_t offset = 0; - uint16_t blockno = 1, rblockno, port = 0; + unsigned short blockno = 1, rblockno, port = 0; int packetlen, sd, fd, retry = 0, ret, result = -1; sd = init_tftp(&server); - packet = (uint8_t*)xzalloc(TFTP_IOBUFSIZE); + packet = xzalloc(TFTP_IOBUFSIZE); fd = xopenro(TT.l); for (;;) { //first loop for request send and confirmation from server. @@ -395,7 +390,7 @@ int file_put(void) ret = write_server(sd, packet, packetlen, &server); if (ret != packetlen) goto errout_with_sd; if (read_ack(sd, packet, &server, &port, NULL) == 0) break; - if (++retry > TFTP_RETRIES) { + if (++retry > TT.R) { error_msg("Retry count exceeded."); goto errout_with_sd; } @@ -416,7 +411,7 @@ int file_put(void) continue; } } - if (++retry > TFTP_RETRIES) { + if (++retry > TT.R) { error_msg("Retry count exceeded."); goto errout_with_sd; } @@ -433,13 +428,13 @@ void tftp_main(void) struct addrinfo *info, rp, *res=0; int ret; - if (FLAG(r)) { - if (!FLAG(l)) { - char *slash = strrchr(TT.r, '/'); - TT.l = (slash) ? slash + 1 : TT.r; + if (TT.r) { + if (!TT.l) { + TT.l = strrchr(TT.r, '/'); + if (!TT.l++) TT.l = TT.r; } - } else if (FLAG(l)) TT.r = TT.l; - else error_exit("Please provide some files."); + } else if (TT.l) TT.r = TT.l; + else error_exit("need -r or -l"); memset(&rp, 0, sizeof(rp)); rp.ai_family = AF_UNSPEC; -- 2.39.5