Bernhard Reutner-Fischer | dac7ff1 | 2006-04-12 17:55:51 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* ------------------------------------------------------------------------- |
| 3 | * tftp.c |
| 4 | * |
| 5 | * A simple tftp client for busybox. |
| 6 | * Tries to follow RFC1350. |
| 7 | * Only "octet" mode supported. |
| 8 | * Optional blocksize negotiation (RFC2347 + RFC2348) |
| 9 | * |
| 10 | * Copyright (C) 2001 Magnus Damm <damm@opensource.se> |
| 11 | * |
| 12 | * Parts of the code based on: |
| 13 | * |
| 14 | * atftp: Copyright (C) 2000 Jean-Pierre Lefebvre <helix@step.polymtl.ca> |
| 15 | * and Remi Lefebvre <remi@debian.org> |
| 16 | * |
| 17 | * utftp: Copyright (C) 1999 Uwe Ohse <uwe@ohse.de> |
| 18 | * |
| 19 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 20 | * ------------------------------------------------------------------------- */ |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 21 | |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 22 | #include "busybox.h" |
| 23 | |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 24 | |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 25 | #define TFTP_BLOCKSIZE_DEFAULT 512 /* according to RFC 1350, don't change */ |
| 26 | #define TFTP_TIMEOUT 5 /* seconds */ |
| 27 | #define TFTP_NUM_RETRIES 5 /* number of retries */ |
| 28 | |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 29 | static const char * const MODE_OCTET = "octet"; |
| 30 | #define MODE_OCTET_LEN 6 /* sizeof(MODE_OCTET)*/ |
| 31 | |
| 32 | static const char * const OPTION_BLOCKSIZE = "blksize"; |
| 33 | #define OPTION_BLOCKSIZE_LEN 8 /* sizeof(OPTION_BLOCKSIZE) */ |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 34 | |
| 35 | /* opcodes we support */ |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 36 | #define TFTP_RRQ 1 |
| 37 | #define TFTP_WRQ 2 |
| 38 | #define TFTP_DATA 3 |
| 39 | #define TFTP_ACK 4 |
| 40 | #define TFTP_ERROR 5 |
| 41 | #define TFTP_OACK 6 |
| 42 | |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 43 | static const char *const tftp_bb_error_msg[] = { |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 44 | "Undefined error", |
| 45 | "File not found", |
| 46 | "Access violation", |
| 47 | "Disk full or allocation error", |
| 48 | "Illegal TFTP operation", |
| 49 | "Unknown transfer ID", |
| 50 | "File already exists", |
| 51 | "No such user" |
| 52 | }; |
| 53 | |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 54 | #define tftp_cmd_get ENABLE_FEATURE_TFTP_GET |
| 55 | |
| 56 | #if ENABLE_FEATURE_TFTP_PUT |
| 57 | # define tftp_cmd_put (tftp_cmd_get+ENABLE_FEATURE_TFTP_PUT) |
"Vladimir N. Oleynik" | 86ac072 | 2005-10-17 10:47:19 +0000 | [diff] [blame] | 58 | #else |
| 59 | # define tftp_cmd_put 0 |
| 60 | #endif |
| 61 | |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 62 | |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 63 | #if ENABLE_FEATURE_TFTP_BLOCKSIZE |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 64 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 65 | static int tftp_blocksize_check(int blocksize, int bufsize) |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 66 | { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 67 | /* Check if the blocksize is valid: |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 68 | * RFC2348 says between 8 and 65464, |
| 69 | * but our implementation makes it impossible |
| 70 | * to use blocksizes smaller than 22 octets. |
| 71 | */ |
| 72 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 73 | if ((bufsize && (blocksize > bufsize)) || |
Rob Landley | 14d7065 | 2006-06-18 15:23:13 +0000 | [diff] [blame] | 74 | (blocksize < 8) || (blocksize > 65564)) { |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 75 | bb_error_msg("bad blocksize"); |
| 76 | return 0; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | return blocksize; |
| 80 | } |
| 81 | |
Bernhard Reutner-Fischer | 32bf1f9 | 2006-06-14 17:29:10 +0000 | [diff] [blame] | 82 | static char *tftp_option_get(char *buf, int len, const char * const option) |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 83 | { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 84 | int opt_val = 0; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 85 | int opt_found = 0; |
| 86 | int k; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 87 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 88 | while (len > 0) { |
| 89 | |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 90 | /* Make sure the options are terminated correctly */ |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 91 | |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 92 | for (k = 0; k < len; k++) { |
| 93 | if (buf[k] == '\0') { |
| 94 | break; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
| 98 | if (k >= len) { |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 99 | break; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | if (opt_val == 0) { |
| 103 | if (strcasecmp(buf, option) == 0) { |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 104 | opt_found = 1; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 105 | } |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 106 | } else { |
| 107 | if (opt_found) { |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 108 | return buf; |
| 109 | } |
| 110 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 111 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 112 | k++; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 113 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 114 | buf += k; |
| 115 | len -= k; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 116 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 117 | opt_val ^= 1; |
| 118 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 119 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 120 | return NULL; |
| 121 | } |
| 122 | |
| 123 | #endif |
| 124 | |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 125 | static int tftp(const int cmd, const struct hostent *host, |
| 126 | const char *remotefile, const int localfd, |
| 127 | const unsigned short port, int tftp_bufsize) |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 128 | { |
| 129 | struct sockaddr_in sa; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 130 | struct sockaddr_in from; |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 131 | struct timeval tv; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 132 | socklen_t fromlen; |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 133 | fd_set rfds; |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 134 | int socketfd; |
Bernhard Reutner-Fischer | 62f9856 | 2006-06-10 14:32:56 +0000 | [diff] [blame] | 135 | int len; |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 136 | int opcode = 0; |
| 137 | int finished = 0; |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 138 | int timeout = TFTP_NUM_RETRIES; |
Glenn L McGrath | c699778 | 2004-02-22 03:33:53 +0000 | [diff] [blame] | 139 | unsigned short block_nr = 1; |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 140 | unsigned short tmp; |
| 141 | char *cp; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 142 | |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 143 | USE_FEATURE_TFTP_BLOCKSIZE(int want_option_ack = 0;) |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 144 | |
Eric Andersen | 744a194 | 2001-11-10 11:16:39 +0000 | [diff] [blame] | 145 | /* Can't use RESERVE_CONFIG_BUFFER here since the allocation |
| 146 | * size varies meaning BUFFERS_GO_ON_STACK would fail */ |
Denis Vlasenko | 10f7dd1 | 2006-12-17 01:14:08 +0000 | [diff] [blame] | 147 | /* We must keep the transmit and receive buffers seperate */ |
| 148 | /* In case we rcv a garbage pkt and we need to rexmit the last pkt */ |
| 149 | char *xbuf = xmalloc(tftp_bufsize += 4); |
| 150 | char *rbuf = xmalloc(tftp_bufsize); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 151 | |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 152 | if ((socketfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) { |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 153 | /* need to unlink the localfile, so don't use xsocket here. */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 154 | bb_perror_msg("socket"); |
Mark Whitley | 8bb7df4 | 2001-03-06 20:58:48 +0000 | [diff] [blame] | 155 | return EXIT_FAILURE; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | len = sizeof(sa); |
| 159 | |
| 160 | memset(&sa, 0, len); |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 161 | xbind(socketfd, (struct sockaddr *)&sa, len); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 162 | |
| 163 | sa.sin_family = host->h_addrtype; |
Glenn L McGrath | 036dbaa | 2004-01-17 05:03:31 +0000 | [diff] [blame] | 164 | sa.sin_port = port; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 165 | memcpy(&sa.sin_addr, (struct in_addr *) host->h_addr, |
| 166 | sizeof(sa.sin_addr)); |
| 167 | |
| 168 | /* build opcode */ |
Bernhard Reutner-Fischer | 32bf1f9 | 2006-06-14 17:29:10 +0000 | [diff] [blame] | 169 | if (cmd & tftp_cmd_get) { |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 170 | opcode = TFTP_RRQ; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 171 | } |
Bernhard Reutner-Fischer | 32bf1f9 | 2006-06-14 17:29:10 +0000 | [diff] [blame] | 172 | if (cmd & tftp_cmd_put) { |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 173 | opcode = TFTP_WRQ; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | while (1) { |
| 177 | |
Denis Vlasenko | 10f7dd1 | 2006-12-17 01:14:08 +0000 | [diff] [blame] | 178 | cp = xbuf; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 179 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 180 | /* first create the opcode part */ |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 181 | *((unsigned short *) cp) = htons(opcode); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 182 | cp += 2; |
| 183 | |
| 184 | /* add filename and mode */ |
Bernhard Reutner-Fischer | 32bf1f9 | 2006-06-14 17:29:10 +0000 | [diff] [blame] | 185 | if (((cmd & tftp_cmd_get) && (opcode == TFTP_RRQ)) || |
| 186 | ((cmd & tftp_cmd_put) && (opcode == TFTP_WRQ))) |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 187 | { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 188 | int too_long = 0; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 189 | |
Denis Vlasenko | 10f7dd1 | 2006-12-17 01:14:08 +0000 | [diff] [blame] | 190 | /* see if the filename fits into xbuf |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 191 | * and fill in packet. */ |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 192 | len = strlen(remotefile) + 1; |
| 193 | |
Denis Vlasenko | 10f7dd1 | 2006-12-17 01:14:08 +0000 | [diff] [blame] | 194 | if ((cp + len) >= &xbuf[tftp_bufsize - 1]) { |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 195 | too_long = 1; |
| 196 | } else { |
| 197 | safe_strncpy(cp, remotefile, len); |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 198 | cp += len; |
| 199 | } |
| 200 | |
Denis Vlasenko | 10f7dd1 | 2006-12-17 01:14:08 +0000 | [diff] [blame] | 201 | if (too_long || ((&xbuf[tftp_bufsize - 1] - cp) < MODE_OCTET_LEN)) { |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 202 | bb_error_msg("remote filename too long"); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 203 | break; |
| 204 | } |
| 205 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 206 | /* add "mode" part of the package */ |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 207 | memcpy(cp, MODE_OCTET, MODE_OCTET_LEN); |
| 208 | cp += MODE_OCTET_LEN; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 209 | |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 210 | #if ENABLE_FEATURE_TFTP_BLOCKSIZE |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 211 | |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 212 | len = tftp_bufsize - 4; /* data block size */ |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 213 | |
| 214 | if (len != TFTP_BLOCKSIZE_DEFAULT) { |
| 215 | |
Denis Vlasenko | 10f7dd1 | 2006-12-17 01:14:08 +0000 | [diff] [blame] | 216 | if ((&xbuf[tftp_bufsize - 1] - cp) < 15) { |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 217 | bb_error_msg("remote filename too long"); |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 218 | break; |
| 219 | } |
| 220 | |
| 221 | /* add "blksize" + number of blocks */ |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 222 | memcpy(cp, OPTION_BLOCKSIZE, OPTION_BLOCKSIZE_LEN); |
| 223 | cp += OPTION_BLOCKSIZE_LEN; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 224 | cp += snprintf(cp, 6, "%d", len) + 1; |
| 225 | |
| 226 | want_option_ack = 1; |
| 227 | } |
| 228 | #endif |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /* add ack and data */ |
| 232 | |
Bernhard Reutner-Fischer | 32bf1f9 | 2006-06-14 17:29:10 +0000 | [diff] [blame] | 233 | if (((cmd & tftp_cmd_get) && (opcode == TFTP_ACK)) || |
| 234 | ((cmd & tftp_cmd_put) && (opcode == TFTP_DATA))) { |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 235 | |
| 236 | *((unsigned short *) cp) = htons(block_nr); |
| 237 | |
| 238 | cp += 2; |
| 239 | |
| 240 | block_nr++; |
| 241 | |
Bernhard Reutner-Fischer | 32bf1f9 | 2006-06-14 17:29:10 +0000 | [diff] [blame] | 242 | if ((cmd & tftp_cmd_put) && (opcode == TFTP_DATA)) { |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 243 | len = full_read(localfd, cp, tftp_bufsize - 4); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 244 | |
| 245 | if (len < 0) { |
Bernhard Reutner-Fischer | 1b9d7c9 | 2006-06-03 22:45:37 +0000 | [diff] [blame] | 246 | bb_perror_msg(bb_msg_read_error); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 247 | break; |
| 248 | } |
| 249 | |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 250 | if (len != (tftp_bufsize - 4)) { |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 251 | finished++; |
| 252 | } |
| 253 | |
| 254 | cp += len; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
| 258 | |
| 259 | /* send packet */ |
| 260 | |
| 261 | |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 262 | timeout = TFTP_NUM_RETRIES; /* re-initialize */ |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 263 | do { |
| 264 | |
Denis Vlasenko | 10f7dd1 | 2006-12-17 01:14:08 +0000 | [diff] [blame] | 265 | len = cp - xbuf; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 266 | |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 267 | #if ENABLE_DEBUG_TFTP |
Glenn L McGrath | d33278d | 2004-02-22 07:20:25 +0000 | [diff] [blame] | 268 | fprintf(stderr, "sending %u bytes\n", len); |
Denis Vlasenko | 10f7dd1 | 2006-12-17 01:14:08 +0000 | [diff] [blame] | 269 | for (cp = xbuf; cp < &xbuf[len]; cp++) |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 270 | fprintf(stderr, "%02x ", (unsigned char) *cp); |
Glenn L McGrath | d33278d | 2004-02-22 07:20:25 +0000 | [diff] [blame] | 271 | fprintf(stderr, "\n"); |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 272 | #endif |
Denis Vlasenko | 10f7dd1 | 2006-12-17 01:14:08 +0000 | [diff] [blame] | 273 | if (sendto(socketfd, xbuf, len, 0, |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 274 | (struct sockaddr *) &sa, sizeof(sa)) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 275 | bb_perror_msg("send"); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 276 | len = -1; |
| 277 | break; |
| 278 | } |
| 279 | |
| 280 | |
Eric Andersen | b99aec0 | 2003-07-30 07:16:39 +0000 | [diff] [blame] | 281 | if (finished && (opcode == TFTP_ACK)) { |
Glenn L McGrath | 0f18271 | 2002-12-19 20:16:22 +0000 | [diff] [blame] | 282 | break; |
| 283 | } |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 284 | |
Glenn L McGrath | 0f18271 | 2002-12-19 20:16:22 +0000 | [diff] [blame] | 285 | /* receive packet */ |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 286 | |
| 287 | memset(&from, 0, sizeof(from)); |
| 288 | fromlen = sizeof(from); |
| 289 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 290 | tv.tv_sec = TFTP_TIMEOUT; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 291 | tv.tv_usec = 0; |
| 292 | |
| 293 | FD_ZERO(&rfds); |
| 294 | FD_SET(socketfd, &rfds); |
| 295 | |
Bernhard Reutner-Fischer | 62f9856 | 2006-06-10 14:32:56 +0000 | [diff] [blame] | 296 | switch (select(socketfd + 1, &rfds, NULL, NULL, &tv)) { |
| 297 | case 1: |
Denis Vlasenko | 10f7dd1 | 2006-12-17 01:14:08 +0000 | [diff] [blame] | 298 | len = recvfrom(socketfd, rbuf, tftp_bufsize, 0, |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 299 | (struct sockaddr *) &from, &fromlen); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 300 | |
| 301 | if (len < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 302 | bb_perror_msg("recvfrom"); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 303 | break; |
| 304 | } |
| 305 | |
| 306 | timeout = 0; |
| 307 | |
Glenn L McGrath | 036dbaa | 2004-01-17 05:03:31 +0000 | [diff] [blame] | 308 | if (sa.sin_port == port) { |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 309 | sa.sin_port = from.sin_port; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 310 | } |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 311 | if (sa.sin_port == from.sin_port) { |
| 312 | break; |
| 313 | } |
| 314 | |
| 315 | /* fall-through for bad packets! */ |
| 316 | /* discard the packet - treat as timeout */ |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 317 | timeout = TFTP_NUM_RETRIES; |
Bernhard Reutner-Fischer | 62f9856 | 2006-06-10 14:32:56 +0000 | [diff] [blame] | 318 | case 0: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 319 | bb_error_msg("timeout"); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 320 | |
Eric Andersen | b99aec0 | 2003-07-30 07:16:39 +0000 | [diff] [blame] | 321 | timeout--; |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 322 | if (timeout == 0) { |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 323 | len = -1; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 324 | bb_error_msg("last timeout"); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 325 | } |
| 326 | break; |
Bernhard Reutner-Fischer | 62f9856 | 2006-06-10 14:32:56 +0000 | [diff] [blame] | 327 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 328 | bb_perror_msg("select"); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 329 | len = -1; |
| 330 | } |
| 331 | |
| 332 | } while (timeout && (len >= 0)); |
| 333 | |
Glenn L McGrath | 0f18271 | 2002-12-19 20:16:22 +0000 | [diff] [blame] | 334 | if ((finished) || (len < 0)) { |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 335 | break; |
| 336 | } |
| 337 | |
| 338 | /* process received packet */ |
| 339 | |
Denis Vlasenko | 10f7dd1 | 2006-12-17 01:14:08 +0000 | [diff] [blame] | 340 | opcode = ntohs(*((unsigned short *) rbuf)); |
| 341 | tmp = ntohs(*((unsigned short *) &rbuf[2])); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 342 | |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 343 | #if ENABLE_DEBUG_TFTP |
Glenn L McGrath | d33278d | 2004-02-22 07:20:25 +0000 | [diff] [blame] | 344 | fprintf(stderr, "received %d bytes: %04x %04x\n", len, opcode, tmp); |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 345 | #endif |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 346 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 347 | if (opcode == TFTP_ERROR) { |
"Vladimir N. Oleynik" | 86ac072 | 2005-10-17 10:47:19 +0000 | [diff] [blame] | 348 | const char *msg = NULL; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 349 | |
Denis Vlasenko | 10f7dd1 | 2006-12-17 01:14:08 +0000 | [diff] [blame] | 350 | if (rbuf[4] != '\0') { |
| 351 | msg = &rbuf[4]; |
| 352 | rbuf[tftp_bufsize - 1] = '\0'; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 353 | } else if (tmp < (sizeof(tftp_bb_error_msg) |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 354 | / sizeof(char *))) { |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 355 | |
"Vladimir N. Oleynik" | 86ac072 | 2005-10-17 10:47:19 +0000 | [diff] [blame] | 356 | msg = tftp_bb_error_msg[tmp]; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | if (msg) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 360 | bb_error_msg("server says: %s", msg); |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | break; |
| 364 | } |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 365 | #if ENABLE_FEATURE_TFTP_BLOCKSIZE |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 366 | if (want_option_ack) { |
| 367 | |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 368 | want_option_ack = 0; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 369 | |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 370 | if (opcode == TFTP_OACK) { |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 371 | |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 372 | /* server seems to support options */ |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 373 | |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 374 | char *res; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 375 | |
Denis Vlasenko | 10f7dd1 | 2006-12-17 01:14:08 +0000 | [diff] [blame] | 376 | res = tftp_option_get(&rbuf[2], len - 2, OPTION_BLOCKSIZE); |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 377 | |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 378 | if (res) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 379 | int blksize = xatoi_u(res); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 380 | |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 381 | if (tftp_blocksize_check(blksize, tftp_bufsize - 4)) { |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 382 | |
Bernhard Reutner-Fischer | 32bf1f9 | 2006-06-14 17:29:10 +0000 | [diff] [blame] | 383 | if (cmd & tftp_cmd_put) { |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 384 | opcode = TFTP_DATA; |
| 385 | } else { |
| 386 | opcode = TFTP_ACK; |
| 387 | } |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 388 | #if ENABLE_DEBUG_TFTP |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 389 | fprintf(stderr, "using %s %u\n", OPTION_BLOCKSIZE, |
| 390 | blksize); |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 391 | #endif |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 392 | tftp_bufsize = blksize + 4; |
| 393 | block_nr = 0; |
| 394 | continue; |
| 395 | } |
| 396 | } |
| 397 | /* FIXME: |
| 398 | * we should send ERROR 8 */ |
| 399 | bb_error_msg("bad server option"); |
| 400 | break; |
| 401 | } |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 402 | |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 403 | bb_error_msg("warning: blksize not supported by server" |
| 404 | " - reverting to 512"); |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 405 | |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 406 | tftp_bufsize = TFTP_BLOCKSIZE_DEFAULT + 4; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 407 | } |
| 408 | #endif |
| 409 | |
Bernhard Reutner-Fischer | 32bf1f9 | 2006-06-14 17:29:10 +0000 | [diff] [blame] | 410 | if ((cmd & tftp_cmd_get) && (opcode == TFTP_DATA)) { |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 411 | |
| 412 | if (tmp == block_nr) { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 413 | |
Denis Vlasenko | 10f7dd1 | 2006-12-17 01:14:08 +0000 | [diff] [blame] | 414 | len = full_write(localfd, &rbuf[4], len - 4); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 415 | |
| 416 | if (len < 0) { |
Bernhard Reutner-Fischer | 1b9d7c9 | 2006-06-03 22:45:37 +0000 | [diff] [blame] | 417 | bb_perror_msg(bb_msg_write_error); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 418 | break; |
| 419 | } |
| 420 | |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 421 | if (len != (tftp_bufsize - 4)) { |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 422 | finished++; |
| 423 | } |
| 424 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 425 | opcode = TFTP_ACK; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 426 | continue; |
| 427 | } |
Rob Landley | f3133c4 | 2005-06-07 02:40:39 +0000 | [diff] [blame] | 428 | /* in case the last ack disappeared into the ether */ |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 429 | if (tmp == (block_nr - 1)) { |
Rob Landley | f3133c4 | 2005-06-07 02:40:39 +0000 | [diff] [blame] | 430 | --block_nr; |
| 431 | opcode = TFTP_ACK; |
| 432 | continue; |
Paul Fox | 1d4c88c | 2005-07-20 19:49:15 +0000 | [diff] [blame] | 433 | } else if (tmp + 1 == block_nr) { |
| 434 | /* Server lost our TFTP_ACK. Resend it */ |
| 435 | block_nr = tmp; |
| 436 | opcode = TFTP_ACK; |
| 437 | continue; |
Rob Landley | f3133c4 | 2005-06-07 02:40:39 +0000 | [diff] [blame] | 438 | } |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Bernhard Reutner-Fischer | 32bf1f9 | 2006-06-14 17:29:10 +0000 | [diff] [blame] | 441 | if ((cmd & tftp_cmd_put) && (opcode == TFTP_ACK)) { |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 442 | |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 443 | if (tmp == (unsigned short) (block_nr - 1)) { |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 444 | if (finished) { |
| 445 | break; |
| 446 | } |
| 447 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 448 | opcode = TFTP_DATA; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 449 | continue; |
| 450 | } |
| 451 | } |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 454 | #if ENABLE_FEATURE_CLEAN_UP |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 455 | close(socketfd); |
Denis Vlasenko | 10f7dd1 | 2006-12-17 01:14:08 +0000 | [diff] [blame] | 456 | free(xbuf); |
| 457 | free(rbuf); |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 458 | #endif |
| 459 | |
Mark Whitley | 8bb7df4 | 2001-03-06 20:58:48 +0000 | [diff] [blame] | 460 | return finished ? EXIT_SUCCESS : EXIT_FAILURE; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | int tftp_main(int argc, char **argv) |
| 464 | { |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 465 | struct hostent *host = NULL; |
Glenn L McGrath | d4004ee | 2004-09-14 17:24:59 +0000 | [diff] [blame] | 466 | const char *localfile = NULL; |
| 467 | const char *remotefile = NULL; |
Glenn L McGrath | 036dbaa | 2004-01-17 05:03:31 +0000 | [diff] [blame] | 468 | int port; |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 469 | int cmd = 0; |
| 470 | int fd = -1; |
| 471 | int flags = 0; |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 472 | int result; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 473 | int blocksize = TFTP_BLOCKSIZE_DEFAULT; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 474 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 475 | /* figure out what to pass to getopt */ |
| 476 | |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 477 | #if ENABLE_FEATURE_TFTP_BLOCKSIZE |
"Vladimir N. Oleynik" | 86ac072 | 2005-10-17 10:47:19 +0000 | [diff] [blame] | 478 | char *sblocksize = NULL; |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 479 | |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 480 | #define BS "b:" |
"Vladimir N. Oleynik" | 86ac072 | 2005-10-17 10:47:19 +0000 | [diff] [blame] | 481 | #define BS_ARG , &sblocksize |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 482 | #else |
| 483 | #define BS |
"Vladimir N. Oleynik" | 86ac072 | 2005-10-17 10:47:19 +0000 | [diff] [blame] | 484 | #define BS_ARG |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 485 | #endif |
| 486 | |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 487 | #if ENABLE_FEATURE_TFTP_GET |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 488 | #define GET "g" |
"Vladimir N. Oleynik" | 86ac072 | 2005-10-17 10:47:19 +0000 | [diff] [blame] | 489 | #define GET_COMPL ":g" |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 490 | #else |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 491 | #define GET |
Bernhard Reutner-Fischer | 886f6af | 2006-04-05 16:47:02 +0000 | [diff] [blame] | 492 | #define GET_COMPL |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 493 | #endif |
| 494 | |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 495 | #if ENABLE_FEATURE_TFTP_PUT |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 496 | #define PUT "p" |
"Vladimir N. Oleynik" | 86ac072 | 2005-10-17 10:47:19 +0000 | [diff] [blame] | 497 | #define PUT_COMPL ":p" |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 498 | #else |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 499 | #define PUT |
"Vladimir N. Oleynik" | 86ac072 | 2005-10-17 10:47:19 +0000 | [diff] [blame] | 500 | #define PUT_COMPL |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 501 | #endif |
| 502 | |
"Vladimir N. Oleynik" | 86ac072 | 2005-10-17 10:47:19 +0000 | [diff] [blame] | 503 | #if defined(CONFIG_FEATURE_TFTP_GET) && defined(CONFIG_FEATURE_TFTP_PUT) |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 504 | opt_complementary = GET_COMPL PUT_COMPL ":?g--p:p--g"; |
"Vladimir N. Oleynik" | 86ac072 | 2005-10-17 10:47:19 +0000 | [diff] [blame] | 505 | #elif defined(CONFIG_FEATURE_TFTP_GET) || defined(CONFIG_FEATURE_TFTP_PUT) |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 506 | opt_complementary = GET_COMPL PUT_COMPL; |
"Vladimir N. Oleynik" | 86ac072 | 2005-10-17 10:47:19 +0000 | [diff] [blame] | 507 | #endif |
| 508 | |
Denis Vlasenko | 000b9ba | 2006-10-05 23:12:49 +0000 | [diff] [blame] | 509 | cmd = getopt32(argc, argv, GET PUT "l:r:" BS, &localfile, &remotefile BS_ARG); |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 510 | |
| 511 | cmd &= (tftp_cmd_get | tftp_cmd_put); |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 512 | #if ENABLE_FEATURE_TFTP_GET |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 513 | if (cmd == tftp_cmd_get) |
| 514 | flags = O_WRONLY | O_CREAT | O_TRUNC; |
| 515 | #endif |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 516 | #if ENABLE_FEATURE_TFTP_PUT |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 517 | if (cmd == tftp_cmd_put) |
| 518 | flags = O_RDONLY; |
| 519 | #endif |
| 520 | |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 521 | #if ENABLE_FEATURE_TFTP_BLOCKSIZE |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 522 | if (sblocksize) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 523 | blocksize = xatoi_u(sblocksize); |
"Vladimir N. Oleynik" | 86ac072 | 2005-10-17 10:47:19 +0000 | [diff] [blame] | 524 | if (!tftp_blocksize_check(blocksize, 0)) { |
| 525 | return EXIT_FAILURE; |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 526 | } |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 527 | } |
"Vladimir N. Oleynik" | 86ac072 | 2005-10-17 10:47:19 +0000 | [diff] [blame] | 528 | #endif |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 529 | |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 530 | if (localfile == NULL) |
| 531 | localfile = remotefile; |
| 532 | if (remotefile == NULL) |
| 533 | remotefile = localfile; |
| 534 | if ((localfile == NULL && remotefile == NULL) || (argv[optind] == NULL)) |
| 535 | bb_show_usage(); |
"Vladimir N. Oleynik" | 86ac072 | 2005-10-17 10:47:19 +0000 | [diff] [blame] | 536 | |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 537 | if (localfile == NULL || LONE_DASH(localfile)) { |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 538 | fd = (cmd == tftp_cmd_get) ? STDOUT_FILENO : STDIN_FILENO; |
| 539 | } else { |
| 540 | fd = open(localfile, flags, 0644); /* fail below */ |
Eric Andersen | a66a43e | 2002-04-13 09:30:25 +0000 | [diff] [blame] | 541 | } |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 542 | if (fd < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 543 | bb_perror_msg_and_die("local file"); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 546 | host = xgethostbyname(argv[optind]); |
Glenn L McGrath | 036dbaa | 2004-01-17 05:03:31 +0000 | [diff] [blame] | 547 | port = bb_lookup_port(argv[optind + 1], "udp", 69); |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 548 | |
Denis Vlasenko | 04291bc | 2006-11-21 10:15:25 +0000 | [diff] [blame] | 549 | #if ENABLE_DEBUG_TFTP |
Glenn L McGrath | d33278d | 2004-02-22 07:20:25 +0000 | [diff] [blame] | 550 | fprintf(stderr, "using server \"%s\", remotefile \"%s\", " |
Bernhard Reutner-Fischer | b25f98a | 2006-06-10 14:15:03 +0000 | [diff] [blame] | 551 | "localfile \"%s\".\n", |
| 552 | inet_ntoa(*((struct in_addr *) host->h_addr)), |
| 553 | remotefile, localfile); |
Eric Andersen | 76fa8ea | 2001-08-20 17:47:49 +0000 | [diff] [blame] | 554 | #endif |
| 555 | |
| 556 | result = tftp(cmd, host, remotefile, fd, port, blocksize); |
Mark Whitley | 450736c | 2001-03-02 19:08:50 +0000 | [diff] [blame] | 557 | |
Eric Andersen | 70060d2 | 2004-03-27 10:02:48 +0000 | [diff] [blame] | 558 | if (!(fd == STDOUT_FILENO || fd == STDIN_FILENO)) { |
Bernhard Reutner-Fischer | 32bf1f9 | 2006-06-14 17:29:10 +0000 | [diff] [blame] | 559 | if (ENABLE_FEATURE_CLEAN_UP) |
| 560 | close(fd); |
| 561 | if (cmd == tftp_cmd_get && result != EXIT_SUCCESS) |
| 562 | unlink(localfile); |
Eric Andersen | a66a43e | 2002-04-13 09:30:25 +0000 | [diff] [blame] | 563 | } |
Denis Vlasenko | 000b9ba | 2006-10-05 23:12:49 +0000 | [diff] [blame] | 564 | return result; |
Glenn L McGrath | ad117d8 | 2001-10-05 04:40:37 +0000 | [diff] [blame] | 565 | } |