tftp.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #ifndef CURL_DISABLE_TFTP
  26. #ifdef HAVE_NETINET_IN_H
  27. #include <netinet/in.h>
  28. #endif
  29. #ifdef HAVE_NETDB_H
  30. #include <netdb.h>
  31. #endif
  32. #ifdef HAVE_ARPA_INET_H
  33. #include <arpa/inet.h>
  34. #endif
  35. #ifdef HAVE_NET_IF_H
  36. #include <net/if.h>
  37. #endif
  38. #ifdef HAVE_SYS_IOCTL_H
  39. #include <sys/ioctl.h>
  40. #endif
  41. #ifdef HAVE_SYS_PARAM_H
  42. #include <sys/param.h>
  43. #endif
  44. #include "urldata.h"
  45. #include <curl/curl.h>
  46. #include "transfer.h"
  47. #include "sendf.h"
  48. #include "tftp.h"
  49. #include "progress.h"
  50. #include "connect.h"
  51. #include "strerror.h"
  52. #include "sockaddr.h" /* required for Curl_sockaddr_storage */
  53. #include "multiif.h"
  54. #include "url.h"
  55. #include "strcase.h"
  56. #include "speedcheck.h"
  57. #include "select.h"
  58. #include "escape.h"
  59. /* The last 3 #include files should be in this order */
  60. #include "curl_printf.h"
  61. #include "curl_memory.h"
  62. #include "memdebug.h"
  63. /* RFC2348 allows the block size to be negotiated */
  64. #define TFTP_BLKSIZE_DEFAULT 512
  65. #define TFTP_BLKSIZE_MIN 8
  66. #define TFTP_BLKSIZE_MAX 65464
  67. #define TFTP_OPTION_BLKSIZE "blksize"
  68. /* from RFC2349: */
  69. #define TFTP_OPTION_TSIZE "tsize"
  70. #define TFTP_OPTION_INTERVAL "timeout"
  71. typedef enum {
  72. TFTP_MODE_NETASCII = 0,
  73. TFTP_MODE_OCTET
  74. } tftp_mode_t;
  75. typedef enum {
  76. TFTP_STATE_START = 0,
  77. TFTP_STATE_RX,
  78. TFTP_STATE_TX,
  79. TFTP_STATE_FIN
  80. } tftp_state_t;
  81. typedef enum {
  82. TFTP_EVENT_NONE = -1,
  83. TFTP_EVENT_INIT = 0,
  84. TFTP_EVENT_RRQ = 1,
  85. TFTP_EVENT_WRQ = 2,
  86. TFTP_EVENT_DATA = 3,
  87. TFTP_EVENT_ACK = 4,
  88. TFTP_EVENT_ERROR = 5,
  89. TFTP_EVENT_OACK = 6,
  90. TFTP_EVENT_TIMEOUT
  91. } tftp_event_t;
  92. typedef enum {
  93. TFTP_ERR_UNDEF = 0,
  94. TFTP_ERR_NOTFOUND,
  95. TFTP_ERR_PERM,
  96. TFTP_ERR_DISKFULL,
  97. TFTP_ERR_ILLEGAL,
  98. TFTP_ERR_UNKNOWNID,
  99. TFTP_ERR_EXISTS,
  100. TFTP_ERR_NOSUCHUSER, /* This will never be triggered by this code */
  101. /* The remaining error codes are internal to curl */
  102. TFTP_ERR_NONE = -100,
  103. TFTP_ERR_TIMEOUT,
  104. TFTP_ERR_NORESPONSE
  105. } tftp_error_t;
  106. struct tftp_packet {
  107. unsigned char *data;
  108. };
  109. struct tftp_state_data {
  110. tftp_state_t state;
  111. tftp_mode_t mode;
  112. tftp_error_t error;
  113. tftp_event_t event;
  114. struct Curl_easy *data;
  115. curl_socket_t sockfd;
  116. int retries;
  117. int retry_time;
  118. int retry_max;
  119. time_t rx_time;
  120. struct Curl_sockaddr_storage local_addr;
  121. struct Curl_sockaddr_storage remote_addr;
  122. curl_socklen_t remote_addrlen;
  123. int rbytes;
  124. int sbytes;
  125. int blksize;
  126. int requested_blksize;
  127. unsigned short block;
  128. struct tftp_packet rpacket;
  129. struct tftp_packet spacket;
  130. };
  131. /* Forward declarations */
  132. static CURLcode tftp_rx(struct tftp_state_data *state, tftp_event_t event);
  133. static CURLcode tftp_tx(struct tftp_state_data *state, tftp_event_t event);
  134. static CURLcode tftp_connect(struct Curl_easy *data, bool *done);
  135. static CURLcode tftp_disconnect(struct Curl_easy *data,
  136. struct connectdata *conn,
  137. bool dead_connection);
  138. static CURLcode tftp_do(struct Curl_easy *data, bool *done);
  139. static CURLcode tftp_done(struct Curl_easy *data,
  140. CURLcode, bool premature);
  141. static CURLcode tftp_setup_connection(struct Curl_easy *data,
  142. struct connectdata *conn);
  143. static CURLcode tftp_multi_statemach(struct Curl_easy *data, bool *done);
  144. static CURLcode tftp_doing(struct Curl_easy *data, bool *dophase_done);
  145. static int tftp_getsock(struct Curl_easy *data, struct connectdata *conn,
  146. curl_socket_t *socks);
  147. static CURLcode tftp_translate_code(tftp_error_t error);
  148. /*
  149. * TFTP protocol handler.
  150. */
  151. const struct Curl_handler Curl_handler_tftp = {
  152. "TFTP", /* scheme */
  153. tftp_setup_connection, /* setup_connection */
  154. tftp_do, /* do_it */
  155. tftp_done, /* done */
  156. ZERO_NULL, /* do_more */
  157. tftp_connect, /* connect_it */
  158. tftp_multi_statemach, /* connecting */
  159. tftp_doing, /* doing */
  160. tftp_getsock, /* proto_getsock */
  161. tftp_getsock, /* doing_getsock */
  162. ZERO_NULL, /* domore_getsock */
  163. ZERO_NULL, /* perform_getsock */
  164. tftp_disconnect, /* disconnect */
  165. ZERO_NULL, /* readwrite */
  166. ZERO_NULL, /* connection_check */
  167. ZERO_NULL, /* attach connection */
  168. PORT_TFTP, /* defport */
  169. CURLPROTO_TFTP, /* protocol */
  170. CURLPROTO_TFTP, /* family */
  171. PROTOPT_NOTCPPROXY | PROTOPT_NOURLQUERY /* flags */
  172. };
  173. /**********************************************************
  174. *
  175. * tftp_set_timeouts -
  176. *
  177. * Set timeouts based on state machine state.
  178. * Use user provided connect timeouts until DATA or ACK
  179. * packet is received, then use user-provided transfer timeouts
  180. *
  181. *
  182. **********************************************************/
  183. static CURLcode tftp_set_timeouts(struct tftp_state_data *state)
  184. {
  185. time_t maxtime, timeout;
  186. timediff_t timeout_ms;
  187. bool start = (state->state == TFTP_STATE_START) ? TRUE : FALSE;
  188. /* Compute drop-dead time */
  189. timeout_ms = Curl_timeleft(state->data, NULL, start);
  190. if(timeout_ms < 0) {
  191. /* time-out, bail out, go home */
  192. failf(state->data, "Connection time-out");
  193. return CURLE_OPERATION_TIMEDOUT;
  194. }
  195. if(timeout_ms > 0)
  196. maxtime = (time_t)(timeout_ms + 500) / 1000;
  197. else
  198. maxtime = 3600; /* use for calculating block timeouts */
  199. /* Set per-block timeout to total */
  200. timeout = maxtime;
  201. /* Average reposting an ACK after 5 seconds */
  202. state->retry_max = (int)timeout/5;
  203. /* But bound the total number */
  204. if(state->retry_max<3)
  205. state->retry_max = 3;
  206. if(state->retry_max>50)
  207. state->retry_max = 50;
  208. /* Compute the re-ACK interval to suit the timeout */
  209. state->retry_time = (int)(timeout/state->retry_max);
  210. if(state->retry_time<1)
  211. state->retry_time = 1;
  212. infof(state->data,
  213. "set timeouts for state %d; Total % " CURL_FORMAT_CURL_OFF_T
  214. ", retry %d maxtry %d",
  215. (int)state->state, timeout_ms, state->retry_time, state->retry_max);
  216. /* init RX time */
  217. time(&state->rx_time);
  218. return CURLE_OK;
  219. }
  220. /**********************************************************
  221. *
  222. * tftp_set_send_first
  223. *
  224. * Event handler for the START state
  225. *
  226. **********************************************************/
  227. static void setpacketevent(struct tftp_packet *packet, unsigned short num)
  228. {
  229. packet->data[0] = (unsigned char)(num >> 8);
  230. packet->data[1] = (unsigned char)(num & 0xff);
  231. }
  232. static void setpacketblock(struct tftp_packet *packet, unsigned short num)
  233. {
  234. packet->data[2] = (unsigned char)(num >> 8);
  235. packet->data[3] = (unsigned char)(num & 0xff);
  236. }
  237. static unsigned short getrpacketevent(const struct tftp_packet *packet)
  238. {
  239. return (unsigned short)((packet->data[0] << 8) | packet->data[1]);
  240. }
  241. static unsigned short getrpacketblock(const struct tftp_packet *packet)
  242. {
  243. return (unsigned short)((packet->data[2] << 8) | packet->data[3]);
  244. }
  245. static size_t tftp_strnlen(const char *string, size_t maxlen)
  246. {
  247. const char *end = memchr(string, '\0', maxlen);
  248. return end ? (size_t) (end - string) : maxlen;
  249. }
  250. static const char *tftp_option_get(const char *buf, size_t len,
  251. const char **option, const char **value)
  252. {
  253. size_t loc;
  254. loc = tftp_strnlen(buf, len);
  255. loc++; /* NULL term */
  256. if(loc >= len)
  257. return NULL;
  258. *option = buf;
  259. loc += tftp_strnlen(buf + loc, len-loc);
  260. loc++; /* NULL term */
  261. if(loc > len)
  262. return NULL;
  263. *value = &buf[strlen(*option) + 1];
  264. return &buf[loc];
  265. }
  266. static CURLcode tftp_parse_option_ack(struct tftp_state_data *state,
  267. const char *ptr, int len)
  268. {
  269. const char *tmp = ptr;
  270. struct Curl_easy *data = state->data;
  271. /* if OACK doesn't contain blksize option, the default (512) must be used */
  272. state->blksize = TFTP_BLKSIZE_DEFAULT;
  273. while(tmp < ptr + len) {
  274. const char *option, *value;
  275. tmp = tftp_option_get(tmp, ptr + len - tmp, &option, &value);
  276. if(!tmp) {
  277. failf(data, "Malformed ACK packet, rejecting");
  278. return CURLE_TFTP_ILLEGAL;
  279. }
  280. infof(data, "got option=(%s) value=(%s)", option, value);
  281. if(checkprefix(TFTP_OPTION_BLKSIZE, option)) {
  282. long blksize;
  283. blksize = strtol(value, NULL, 10);
  284. if(!blksize) {
  285. failf(data, "invalid blocksize value in OACK packet");
  286. return CURLE_TFTP_ILLEGAL;
  287. }
  288. if(blksize > TFTP_BLKSIZE_MAX) {
  289. failf(data, "%s (%d)", "blksize is larger than max supported",
  290. TFTP_BLKSIZE_MAX);
  291. return CURLE_TFTP_ILLEGAL;
  292. }
  293. else if(blksize < TFTP_BLKSIZE_MIN) {
  294. failf(data, "%s (%d)", "blksize is smaller than min supported",
  295. TFTP_BLKSIZE_MIN);
  296. return CURLE_TFTP_ILLEGAL;
  297. }
  298. else if(blksize > state->requested_blksize) {
  299. /* could realloc pkt buffers here, but the spec doesn't call out
  300. * support for the server requesting a bigger blksize than the client
  301. * requests */
  302. failf(data, "%s (%ld)",
  303. "server requested blksize larger than allocated", blksize);
  304. return CURLE_TFTP_ILLEGAL;
  305. }
  306. state->blksize = (int)blksize;
  307. infof(data, "%s (%d) %s (%d)", "blksize parsed from OACK",
  308. state->blksize, "requested", state->requested_blksize);
  309. }
  310. else if(checkprefix(TFTP_OPTION_TSIZE, option)) {
  311. long tsize = 0;
  312. tsize = strtol(value, NULL, 10);
  313. infof(data, "%s (%ld)", "tsize parsed from OACK", tsize);
  314. /* tsize should be ignored on upload: Who cares about the size of the
  315. remote file? */
  316. if(!data->set.upload) {
  317. if(!tsize) {
  318. failf(data, "invalid tsize -:%s:- value in OACK packet", value);
  319. return CURLE_TFTP_ILLEGAL;
  320. }
  321. Curl_pgrsSetDownloadSize(data, tsize);
  322. }
  323. }
  324. }
  325. return CURLE_OK;
  326. }
  327. static CURLcode tftp_option_add(struct tftp_state_data *state, size_t *csize,
  328. char *buf, const char *option)
  329. {
  330. if(( strlen(option) + *csize + 1) > (size_t)state->blksize)
  331. return CURLE_TFTP_ILLEGAL;
  332. strcpy(buf, option);
  333. *csize += strlen(option) + 1;
  334. return CURLE_OK;
  335. }
  336. static CURLcode tftp_connect_for_tx(struct tftp_state_data *state,
  337. tftp_event_t event)
  338. {
  339. CURLcode result;
  340. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  341. struct Curl_easy *data = state->data;
  342. infof(data, "%s", "Connected for transmit");
  343. #endif
  344. state->state = TFTP_STATE_TX;
  345. result = tftp_set_timeouts(state);
  346. if(result)
  347. return result;
  348. return tftp_tx(state, event);
  349. }
  350. static CURLcode tftp_connect_for_rx(struct tftp_state_data *state,
  351. tftp_event_t event)
  352. {
  353. CURLcode result;
  354. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  355. struct Curl_easy *data = state->data;
  356. infof(data, "%s", "Connected for receive");
  357. #endif
  358. state->state = TFTP_STATE_RX;
  359. result = tftp_set_timeouts(state);
  360. if(result)
  361. return result;
  362. return tftp_rx(state, event);
  363. }
  364. static CURLcode tftp_send_first(struct tftp_state_data *state,
  365. tftp_event_t event)
  366. {
  367. size_t sbytes;
  368. ssize_t senddata;
  369. const char *mode = "octet";
  370. char *filename;
  371. struct Curl_easy *data = state->data;
  372. CURLcode result = CURLE_OK;
  373. /* Set ascii mode if -B flag was used */
  374. if(data->state.prefer_ascii)
  375. mode = "netascii";
  376. switch(event) {
  377. case TFTP_EVENT_INIT: /* Send the first packet out */
  378. case TFTP_EVENT_TIMEOUT: /* Resend the first packet out */
  379. /* Increment the retry counter, quit if over the limit */
  380. state->retries++;
  381. if(state->retries>state->retry_max) {
  382. state->error = TFTP_ERR_NORESPONSE;
  383. state->state = TFTP_STATE_FIN;
  384. return result;
  385. }
  386. if(data->set.upload) {
  387. /* If we are uploading, send an WRQ */
  388. setpacketevent(&state->spacket, TFTP_EVENT_WRQ);
  389. state->data->req.upload_fromhere =
  390. (char *)state->spacket.data + 4;
  391. if(data->state.infilesize != -1)
  392. Curl_pgrsSetUploadSize(data, data->state.infilesize);
  393. }
  394. else {
  395. /* If we are downloading, send an RRQ */
  396. setpacketevent(&state->spacket, TFTP_EVENT_RRQ);
  397. }
  398. /* As RFC3617 describes the separator slash is not actually part of the
  399. file name so we skip the always-present first letter of the path
  400. string. */
  401. result = Curl_urldecode(&state->data->state.up.path[1], 0,
  402. &filename, NULL, REJECT_ZERO);
  403. if(result)
  404. return result;
  405. if(strlen(filename) > (state->blksize - strlen(mode) - 4)) {
  406. failf(data, "TFTP file name too long");
  407. free(filename);
  408. return CURLE_TFTP_ILLEGAL; /* too long file name field */
  409. }
  410. msnprintf((char *)state->spacket.data + 2,
  411. state->blksize,
  412. "%s%c%s%c", filename, '\0', mode, '\0');
  413. sbytes = 4 + strlen(filename) + strlen(mode);
  414. /* optional addition of TFTP options */
  415. if(!data->set.tftp_no_options) {
  416. char buf[64];
  417. /* add tsize option */
  418. if(data->set.upload && (data->state.infilesize != -1))
  419. msnprintf(buf, sizeof(buf), "%" CURL_FORMAT_CURL_OFF_T,
  420. data->state.infilesize);
  421. else
  422. strcpy(buf, "0"); /* the destination is large enough */
  423. result = tftp_option_add(state, &sbytes,
  424. (char *)state->spacket.data + sbytes,
  425. TFTP_OPTION_TSIZE);
  426. if(result == CURLE_OK)
  427. result = tftp_option_add(state, &sbytes,
  428. (char *)state->spacket.data + sbytes, buf);
  429. /* add blksize option */
  430. msnprintf(buf, sizeof(buf), "%d", state->requested_blksize);
  431. if(result == CURLE_OK)
  432. result = tftp_option_add(state, &sbytes,
  433. (char *)state->spacket.data + sbytes,
  434. TFTP_OPTION_BLKSIZE);
  435. if(result == CURLE_OK)
  436. result = tftp_option_add(state, &sbytes,
  437. (char *)state->spacket.data + sbytes, buf);
  438. /* add timeout option */
  439. msnprintf(buf, sizeof(buf), "%d", state->retry_time);
  440. if(result == CURLE_OK)
  441. result = tftp_option_add(state, &sbytes,
  442. (char *)state->spacket.data + sbytes,
  443. TFTP_OPTION_INTERVAL);
  444. if(result == CURLE_OK)
  445. result = tftp_option_add(state, &sbytes,
  446. (char *)state->spacket.data + sbytes, buf);
  447. if(result != CURLE_OK) {
  448. failf(data, "TFTP buffer too small for options");
  449. free(filename);
  450. return CURLE_TFTP_ILLEGAL;
  451. }
  452. }
  453. /* the typecase for the 3rd argument is mostly for systems that do
  454. not have a size_t argument, like older unixes that want an 'int' */
  455. senddata = sendto(state->sockfd, (void *)state->spacket.data,
  456. (SEND_TYPE_ARG3)sbytes, 0,
  457. data->conn->ip_addr->ai_addr,
  458. data->conn->ip_addr->ai_addrlen);
  459. if(senddata != (ssize_t)sbytes) {
  460. char buffer[STRERROR_LEN];
  461. failf(data, "%s", Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
  462. }
  463. free(filename);
  464. break;
  465. case TFTP_EVENT_OACK:
  466. if(data->set.upload) {
  467. result = tftp_connect_for_tx(state, event);
  468. }
  469. else {
  470. result = tftp_connect_for_rx(state, event);
  471. }
  472. break;
  473. case TFTP_EVENT_ACK: /* Connected for transmit */
  474. result = tftp_connect_for_tx(state, event);
  475. break;
  476. case TFTP_EVENT_DATA: /* Connected for receive */
  477. result = tftp_connect_for_rx(state, event);
  478. break;
  479. case TFTP_EVENT_ERROR:
  480. state->state = TFTP_STATE_FIN;
  481. break;
  482. default:
  483. failf(state->data, "tftp_send_first: internal error");
  484. break;
  485. }
  486. return result;
  487. }
  488. /* the next blocknum is x + 1 but it needs to wrap at an unsigned 16bit
  489. boundary */
  490. #define NEXT_BLOCKNUM(x) (((x) + 1)&0xffff)
  491. /**********************************************************
  492. *
  493. * tftp_rx
  494. *
  495. * Event handler for the RX state
  496. *
  497. **********************************************************/
  498. static CURLcode tftp_rx(struct tftp_state_data *state,
  499. tftp_event_t event)
  500. {
  501. ssize_t sbytes;
  502. int rblock;
  503. struct Curl_easy *data = state->data;
  504. char buffer[STRERROR_LEN];
  505. switch(event) {
  506. case TFTP_EVENT_DATA:
  507. /* Is this the block we expect? */
  508. rblock = getrpacketblock(&state->rpacket);
  509. if(NEXT_BLOCKNUM(state->block) == rblock) {
  510. /* This is the expected block. Reset counters and ACK it. */
  511. state->retries = 0;
  512. }
  513. else if(state->block == rblock) {
  514. /* This is the last recently received block again. Log it and ACK it
  515. again. */
  516. infof(data, "Received last DATA packet block %d again.", rblock);
  517. }
  518. else {
  519. /* totally unexpected, just log it */
  520. infof(data,
  521. "Received unexpected DATA packet block %d, expecting block %d",
  522. rblock, NEXT_BLOCKNUM(state->block));
  523. break;
  524. }
  525. /* ACK this block. */
  526. state->block = (unsigned short)rblock;
  527. setpacketevent(&state->spacket, TFTP_EVENT_ACK);
  528. setpacketblock(&state->spacket, state->block);
  529. sbytes = sendto(state->sockfd, (void *)state->spacket.data,
  530. 4, SEND_4TH_ARG,
  531. (struct sockaddr *)&state->remote_addr,
  532. state->remote_addrlen);
  533. if(sbytes < 0) {
  534. failf(data, "%s", Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
  535. return CURLE_SEND_ERROR;
  536. }
  537. /* Check if completed (That is, a less than full packet is received) */
  538. if(state->rbytes < (ssize_t)state->blksize + 4) {
  539. state->state = TFTP_STATE_FIN;
  540. }
  541. else {
  542. state->state = TFTP_STATE_RX;
  543. }
  544. time(&state->rx_time);
  545. break;
  546. case TFTP_EVENT_OACK:
  547. /* ACK option acknowledgement so we can move on to data */
  548. state->block = 0;
  549. state->retries = 0;
  550. setpacketevent(&state->spacket, TFTP_EVENT_ACK);
  551. setpacketblock(&state->spacket, state->block);
  552. sbytes = sendto(state->sockfd, (void *)state->spacket.data,
  553. 4, SEND_4TH_ARG,
  554. (struct sockaddr *)&state->remote_addr,
  555. state->remote_addrlen);
  556. if(sbytes < 0) {
  557. failf(data, "%s", Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
  558. return CURLE_SEND_ERROR;
  559. }
  560. /* we're ready to RX data */
  561. state->state = TFTP_STATE_RX;
  562. time(&state->rx_time);
  563. break;
  564. case TFTP_EVENT_TIMEOUT:
  565. /* Increment the retry count and fail if over the limit */
  566. state->retries++;
  567. infof(data,
  568. "Timeout waiting for block %d ACK. Retries = %d",
  569. NEXT_BLOCKNUM(state->block), state->retries);
  570. if(state->retries > state->retry_max) {
  571. state->error = TFTP_ERR_TIMEOUT;
  572. state->state = TFTP_STATE_FIN;
  573. }
  574. else {
  575. /* Resend the previous ACK */
  576. sbytes = sendto(state->sockfd, (void *)state->spacket.data,
  577. 4, SEND_4TH_ARG,
  578. (struct sockaddr *)&state->remote_addr,
  579. state->remote_addrlen);
  580. if(sbytes<0) {
  581. failf(data, "%s", Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
  582. return CURLE_SEND_ERROR;
  583. }
  584. }
  585. break;
  586. case TFTP_EVENT_ERROR:
  587. setpacketevent(&state->spacket, TFTP_EVENT_ERROR);
  588. setpacketblock(&state->spacket, state->block);
  589. (void)sendto(state->sockfd, (void *)state->spacket.data,
  590. 4, SEND_4TH_ARG,
  591. (struct sockaddr *)&state->remote_addr,
  592. state->remote_addrlen);
  593. /* don't bother with the return code, but if the socket is still up we
  594. * should be a good TFTP client and let the server know we're done */
  595. state->state = TFTP_STATE_FIN;
  596. break;
  597. default:
  598. failf(data, "%s", "tftp_rx: internal error");
  599. return CURLE_TFTP_ILLEGAL; /* not really the perfect return code for
  600. this */
  601. }
  602. return CURLE_OK;
  603. }
  604. /**********************************************************
  605. *
  606. * tftp_tx
  607. *
  608. * Event handler for the TX state
  609. *
  610. **********************************************************/
  611. static CURLcode tftp_tx(struct tftp_state_data *state, tftp_event_t event)
  612. {
  613. struct Curl_easy *data = state->data;
  614. ssize_t sbytes;
  615. CURLcode result = CURLE_OK;
  616. struct SingleRequest *k = &data->req;
  617. size_t cb; /* Bytes currently read */
  618. char buffer[STRERROR_LEN];
  619. switch(event) {
  620. case TFTP_EVENT_ACK:
  621. case TFTP_EVENT_OACK:
  622. if(event == TFTP_EVENT_ACK) {
  623. /* Ack the packet */
  624. int rblock = getrpacketblock(&state->rpacket);
  625. if(rblock != state->block &&
  626. /* There's a bug in tftpd-hpa that causes it to send us an ack for
  627. * 65535 when the block number wraps to 0. So when we're expecting
  628. * 0, also accept 65535. See
  629. * https://www.syslinux.org/archives/2010-September/015612.html
  630. * */
  631. !(state->block == 0 && rblock == 65535)) {
  632. /* This isn't the expected block. Log it and up the retry counter */
  633. infof(data, "Received ACK for block %d, expecting %d",
  634. rblock, state->block);
  635. state->retries++;
  636. /* Bail out if over the maximum */
  637. if(state->retries>state->retry_max) {
  638. failf(data, "tftp_tx: giving up waiting for block %d ack",
  639. state->block);
  640. result = CURLE_SEND_ERROR;
  641. }
  642. else {
  643. /* Re-send the data packet */
  644. sbytes = sendto(state->sockfd, (void *)state->spacket.data,
  645. 4 + state->sbytes, SEND_4TH_ARG,
  646. (struct sockaddr *)&state->remote_addr,
  647. state->remote_addrlen);
  648. /* Check all sbytes were sent */
  649. if(sbytes<0) {
  650. failf(data, "%s", Curl_strerror(SOCKERRNO,
  651. buffer, sizeof(buffer)));
  652. result = CURLE_SEND_ERROR;
  653. }
  654. }
  655. return result;
  656. }
  657. /* This is the expected packet. Reset the counters and send the next
  658. block */
  659. time(&state->rx_time);
  660. state->block++;
  661. }
  662. else
  663. state->block = 1; /* first data block is 1 when using OACK */
  664. state->retries = 0;
  665. setpacketevent(&state->spacket, TFTP_EVENT_DATA);
  666. setpacketblock(&state->spacket, state->block);
  667. if(state->block > 1 && state->sbytes < state->blksize) {
  668. state->state = TFTP_STATE_FIN;
  669. return CURLE_OK;
  670. }
  671. /* TFTP considers data block size < 512 bytes as an end of session. So
  672. * in some cases we must wait for additional data to build full (512 bytes)
  673. * data block.
  674. * */
  675. state->sbytes = 0;
  676. state->data->req.upload_fromhere = (char *)state->spacket.data + 4;
  677. do {
  678. result = Curl_fillreadbuffer(data, state->blksize - state->sbytes, &cb);
  679. if(result)
  680. return result;
  681. state->sbytes += (int)cb;
  682. state->data->req.upload_fromhere += cb;
  683. } while(state->sbytes < state->blksize && cb);
  684. sbytes = sendto(state->sockfd, (void *) state->spacket.data,
  685. 4 + state->sbytes, SEND_4TH_ARG,
  686. (struct sockaddr *)&state->remote_addr,
  687. state->remote_addrlen);
  688. /* Check all sbytes were sent */
  689. if(sbytes<0) {
  690. failf(data, "%s", Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
  691. return CURLE_SEND_ERROR;
  692. }
  693. /* Update the progress meter */
  694. k->writebytecount += state->sbytes;
  695. Curl_pgrsSetUploadCounter(data, k->writebytecount);
  696. break;
  697. case TFTP_EVENT_TIMEOUT:
  698. /* Increment the retry counter and log the timeout */
  699. state->retries++;
  700. infof(data, "Timeout waiting for block %d ACK. "
  701. " Retries = %d", NEXT_BLOCKNUM(state->block), state->retries);
  702. /* Decide if we've had enough */
  703. if(state->retries > state->retry_max) {
  704. state->error = TFTP_ERR_TIMEOUT;
  705. state->state = TFTP_STATE_FIN;
  706. }
  707. else {
  708. /* Re-send the data packet */
  709. sbytes = sendto(state->sockfd, (void *)state->spacket.data,
  710. 4 + state->sbytes, SEND_4TH_ARG,
  711. (struct sockaddr *)&state->remote_addr,
  712. state->remote_addrlen);
  713. /* Check all sbytes were sent */
  714. if(sbytes<0) {
  715. failf(data, "%s", Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
  716. return CURLE_SEND_ERROR;
  717. }
  718. /* since this was a re-send, we remain at the still byte position */
  719. Curl_pgrsSetUploadCounter(data, k->writebytecount);
  720. }
  721. break;
  722. case TFTP_EVENT_ERROR:
  723. state->state = TFTP_STATE_FIN;
  724. setpacketevent(&state->spacket, TFTP_EVENT_ERROR);
  725. setpacketblock(&state->spacket, state->block);
  726. (void)sendto(state->sockfd, (void *)state->spacket.data, 4, SEND_4TH_ARG,
  727. (struct sockaddr *)&state->remote_addr,
  728. state->remote_addrlen);
  729. /* don't bother with the return code, but if the socket is still up we
  730. * should be a good TFTP client and let the server know we're done */
  731. state->state = TFTP_STATE_FIN;
  732. break;
  733. default:
  734. failf(data, "tftp_tx: internal error, event: %i", (int)(event));
  735. break;
  736. }
  737. return result;
  738. }
  739. /**********************************************************
  740. *
  741. * tftp_translate_code
  742. *
  743. * Translate internal error codes to CURL error codes
  744. *
  745. **********************************************************/
  746. static CURLcode tftp_translate_code(tftp_error_t error)
  747. {
  748. CURLcode result = CURLE_OK;
  749. if(error != TFTP_ERR_NONE) {
  750. switch(error) {
  751. case TFTP_ERR_NOTFOUND:
  752. result = CURLE_TFTP_NOTFOUND;
  753. break;
  754. case TFTP_ERR_PERM:
  755. result = CURLE_TFTP_PERM;
  756. break;
  757. case TFTP_ERR_DISKFULL:
  758. result = CURLE_REMOTE_DISK_FULL;
  759. break;
  760. case TFTP_ERR_UNDEF:
  761. case TFTP_ERR_ILLEGAL:
  762. result = CURLE_TFTP_ILLEGAL;
  763. break;
  764. case TFTP_ERR_UNKNOWNID:
  765. result = CURLE_TFTP_UNKNOWNID;
  766. break;
  767. case TFTP_ERR_EXISTS:
  768. result = CURLE_REMOTE_FILE_EXISTS;
  769. break;
  770. case TFTP_ERR_NOSUCHUSER:
  771. result = CURLE_TFTP_NOSUCHUSER;
  772. break;
  773. case TFTP_ERR_TIMEOUT:
  774. result = CURLE_OPERATION_TIMEDOUT;
  775. break;
  776. case TFTP_ERR_NORESPONSE:
  777. result = CURLE_COULDNT_CONNECT;
  778. break;
  779. default:
  780. result = CURLE_ABORTED_BY_CALLBACK;
  781. break;
  782. }
  783. }
  784. else
  785. result = CURLE_OK;
  786. return result;
  787. }
  788. /**********************************************************
  789. *
  790. * tftp_state_machine
  791. *
  792. * The tftp state machine event dispatcher
  793. *
  794. **********************************************************/
  795. static CURLcode tftp_state_machine(struct tftp_state_data *state,
  796. tftp_event_t event)
  797. {
  798. CURLcode result = CURLE_OK;
  799. struct Curl_easy *data = state->data;
  800. switch(state->state) {
  801. case TFTP_STATE_START:
  802. DEBUGF(infof(data, "TFTP_STATE_START"));
  803. result = tftp_send_first(state, event);
  804. break;
  805. case TFTP_STATE_RX:
  806. DEBUGF(infof(data, "TFTP_STATE_RX"));
  807. result = tftp_rx(state, event);
  808. break;
  809. case TFTP_STATE_TX:
  810. DEBUGF(infof(data, "TFTP_STATE_TX"));
  811. result = tftp_tx(state, event);
  812. break;
  813. case TFTP_STATE_FIN:
  814. infof(data, "%s", "TFTP finished");
  815. break;
  816. default:
  817. DEBUGF(infof(data, "STATE: %d", state->state));
  818. failf(data, "%s", "Internal state machine error");
  819. result = CURLE_TFTP_ILLEGAL;
  820. break;
  821. }
  822. return result;
  823. }
  824. /**********************************************************
  825. *
  826. * tftp_disconnect
  827. *
  828. * The disconnect callback
  829. *
  830. **********************************************************/
  831. static CURLcode tftp_disconnect(struct Curl_easy *data,
  832. struct connectdata *conn, bool dead_connection)
  833. {
  834. struct tftp_state_data *state = conn->proto.tftpc;
  835. (void) data;
  836. (void) dead_connection;
  837. /* done, free dynamically allocated pkt buffers */
  838. if(state) {
  839. Curl_safefree(state->rpacket.data);
  840. Curl_safefree(state->spacket.data);
  841. free(state);
  842. }
  843. return CURLE_OK;
  844. }
  845. /**********************************************************
  846. *
  847. * tftp_connect
  848. *
  849. * The connect callback
  850. *
  851. **********************************************************/
  852. static CURLcode tftp_connect(struct Curl_easy *data, bool *done)
  853. {
  854. struct tftp_state_data *state;
  855. int blksize;
  856. int need_blksize;
  857. struct connectdata *conn = data->conn;
  858. blksize = TFTP_BLKSIZE_DEFAULT;
  859. state = conn->proto.tftpc = calloc(1, sizeof(struct tftp_state_data));
  860. if(!state)
  861. return CURLE_OUT_OF_MEMORY;
  862. /* alloc pkt buffers based on specified blksize */
  863. if(data->set.tftp_blksize) {
  864. blksize = (int)data->set.tftp_blksize;
  865. if(blksize > TFTP_BLKSIZE_MAX || blksize < TFTP_BLKSIZE_MIN)
  866. return CURLE_TFTP_ILLEGAL;
  867. }
  868. need_blksize = blksize;
  869. /* default size is the fallback when no OACK is received */
  870. if(need_blksize < TFTP_BLKSIZE_DEFAULT)
  871. need_blksize = TFTP_BLKSIZE_DEFAULT;
  872. if(!state->rpacket.data) {
  873. state->rpacket.data = calloc(1, need_blksize + 2 + 2);
  874. if(!state->rpacket.data)
  875. return CURLE_OUT_OF_MEMORY;
  876. }
  877. if(!state->spacket.data) {
  878. state->spacket.data = calloc(1, need_blksize + 2 + 2);
  879. if(!state->spacket.data)
  880. return CURLE_OUT_OF_MEMORY;
  881. }
  882. /* we don't keep TFTP connections up basically because there's none or very
  883. * little gain for UDP */
  884. connclose(conn, "TFTP");
  885. state->data = data;
  886. state->sockfd = conn->sock[FIRSTSOCKET];
  887. state->state = TFTP_STATE_START;
  888. state->error = TFTP_ERR_NONE;
  889. state->blksize = TFTP_BLKSIZE_DEFAULT; /* Unless updated by OACK response */
  890. state->requested_blksize = blksize;
  891. ((struct sockaddr *)&state->local_addr)->sa_family =
  892. (CURL_SA_FAMILY_T)(conn->ip_addr->ai_family);
  893. tftp_set_timeouts(state);
  894. if(!conn->bits.bound) {
  895. /* If not already bound, bind to any interface, random UDP port. If it is
  896. * reused or a custom local port was desired, this has already been done!
  897. *
  898. * We once used the size of the local_addr struct as the third argument
  899. * for bind() to better work with IPv6 or whatever size the struct could
  900. * have, but we learned that at least Tru64, AIX and IRIX *requires* the
  901. * size of that argument to match the exact size of a 'sockaddr_in' struct
  902. * when running IPv4-only.
  903. *
  904. * Therefore we use the size from the address we connected to, which we
  905. * assume uses the same IP version and thus hopefully this works for both
  906. * IPv4 and IPv6...
  907. */
  908. int rc = bind(state->sockfd, (struct sockaddr *)&state->local_addr,
  909. conn->ip_addr->ai_addrlen);
  910. if(rc) {
  911. char buffer[STRERROR_LEN];
  912. failf(data, "bind() failed; %s",
  913. Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
  914. return CURLE_COULDNT_CONNECT;
  915. }
  916. conn->bits.bound = TRUE;
  917. }
  918. Curl_pgrsStartNow(data);
  919. *done = TRUE;
  920. return CURLE_OK;
  921. }
  922. /**********************************************************
  923. *
  924. * tftp_done
  925. *
  926. * The done callback
  927. *
  928. **********************************************************/
  929. static CURLcode tftp_done(struct Curl_easy *data, CURLcode status,
  930. bool premature)
  931. {
  932. CURLcode result = CURLE_OK;
  933. struct connectdata *conn = data->conn;
  934. struct tftp_state_data *state = conn->proto.tftpc;
  935. (void)status; /* unused */
  936. (void)premature; /* not used */
  937. if(Curl_pgrsDone(data))
  938. return CURLE_ABORTED_BY_CALLBACK;
  939. /* If we have encountered an error */
  940. if(state)
  941. result = tftp_translate_code(state->error);
  942. return result;
  943. }
  944. /**********************************************************
  945. *
  946. * tftp_getsock
  947. *
  948. * The getsock callback
  949. *
  950. **********************************************************/
  951. static int tftp_getsock(struct Curl_easy *data,
  952. struct connectdata *conn, curl_socket_t *socks)
  953. {
  954. (void)data;
  955. socks[0] = conn->sock[FIRSTSOCKET];
  956. return GETSOCK_READSOCK(0);
  957. }
  958. /**********************************************************
  959. *
  960. * tftp_receive_packet
  961. *
  962. * Called once select fires and data is ready on the socket
  963. *
  964. **********************************************************/
  965. static CURLcode tftp_receive_packet(struct Curl_easy *data)
  966. {
  967. struct Curl_sockaddr_storage fromaddr;
  968. curl_socklen_t fromlen;
  969. CURLcode result = CURLE_OK;
  970. struct connectdata *conn = data->conn;
  971. struct tftp_state_data *state = conn->proto.tftpc;
  972. struct SingleRequest *k = &data->req;
  973. /* Receive the packet */
  974. fromlen = sizeof(fromaddr);
  975. state->rbytes = (int)recvfrom(state->sockfd,
  976. (void *)state->rpacket.data,
  977. state->blksize + 4,
  978. 0,
  979. (struct sockaddr *)&fromaddr,
  980. &fromlen);
  981. if(state->remote_addrlen == 0) {
  982. memcpy(&state->remote_addr, &fromaddr, fromlen);
  983. state->remote_addrlen = fromlen;
  984. }
  985. /* Sanity check packet length */
  986. if(state->rbytes < 4) {
  987. failf(data, "Received too short packet");
  988. /* Not a timeout, but how best to handle it? */
  989. state->event = TFTP_EVENT_TIMEOUT;
  990. }
  991. else {
  992. /* The event is given by the TFTP packet time */
  993. unsigned short event = getrpacketevent(&state->rpacket);
  994. state->event = (tftp_event_t)event;
  995. switch(state->event) {
  996. case TFTP_EVENT_DATA:
  997. /* Don't pass to the client empty or retransmitted packets */
  998. if(state->rbytes > 4 &&
  999. (NEXT_BLOCKNUM(state->block) == getrpacketblock(&state->rpacket))) {
  1000. result = Curl_client_write(data, CLIENTWRITE_BODY,
  1001. (char *)state->rpacket.data + 4,
  1002. state->rbytes-4);
  1003. if(result) {
  1004. tftp_state_machine(state, TFTP_EVENT_ERROR);
  1005. return result;
  1006. }
  1007. k->bytecount += state->rbytes-4;
  1008. Curl_pgrsSetDownloadCounter(data, (curl_off_t) k->bytecount);
  1009. }
  1010. break;
  1011. case TFTP_EVENT_ERROR:
  1012. {
  1013. unsigned short error = getrpacketblock(&state->rpacket);
  1014. char *str = (char *)state->rpacket.data + 4;
  1015. size_t strn = state->rbytes - 4;
  1016. state->error = (tftp_error_t)error;
  1017. if(tftp_strnlen(str, strn) < strn)
  1018. infof(data, "TFTP error: %s", str);
  1019. break;
  1020. }
  1021. case TFTP_EVENT_ACK:
  1022. break;
  1023. case TFTP_EVENT_OACK:
  1024. result = tftp_parse_option_ack(state,
  1025. (const char *)state->rpacket.data + 2,
  1026. state->rbytes-2);
  1027. if(result)
  1028. return result;
  1029. break;
  1030. case TFTP_EVENT_RRQ:
  1031. case TFTP_EVENT_WRQ:
  1032. default:
  1033. failf(data, "%s", "Internal error: Unexpected packet");
  1034. break;
  1035. }
  1036. /* Update the progress meter */
  1037. if(Curl_pgrsUpdate(data)) {
  1038. tftp_state_machine(state, TFTP_EVENT_ERROR);
  1039. return CURLE_ABORTED_BY_CALLBACK;
  1040. }
  1041. }
  1042. return result;
  1043. }
  1044. /**********************************************************
  1045. *
  1046. * tftp_state_timeout
  1047. *
  1048. * Check if timeouts have been reached
  1049. *
  1050. **********************************************************/
  1051. static timediff_t tftp_state_timeout(struct Curl_easy *data,
  1052. tftp_event_t *event)
  1053. {
  1054. time_t current;
  1055. struct connectdata *conn = data->conn;
  1056. struct tftp_state_data *state = conn->proto.tftpc;
  1057. timediff_t timeout_ms;
  1058. if(event)
  1059. *event = TFTP_EVENT_NONE;
  1060. timeout_ms = Curl_timeleft(state->data, NULL,
  1061. (state->state == TFTP_STATE_START));
  1062. if(timeout_ms < 0) {
  1063. state->error = TFTP_ERR_TIMEOUT;
  1064. state->state = TFTP_STATE_FIN;
  1065. return 0;
  1066. }
  1067. time(&current);
  1068. if(current > state->rx_time + state->retry_time) {
  1069. if(event)
  1070. *event = TFTP_EVENT_TIMEOUT;
  1071. time(&state->rx_time); /* update even though we received nothing */
  1072. }
  1073. return timeout_ms;
  1074. }
  1075. /**********************************************************
  1076. *
  1077. * tftp_multi_statemach
  1078. *
  1079. * Handle single RX socket event and return
  1080. *
  1081. **********************************************************/
  1082. static CURLcode tftp_multi_statemach(struct Curl_easy *data, bool *done)
  1083. {
  1084. tftp_event_t event;
  1085. CURLcode result = CURLE_OK;
  1086. struct connectdata *conn = data->conn;
  1087. struct tftp_state_data *state = conn->proto.tftpc;
  1088. timediff_t timeout_ms = tftp_state_timeout(data, &event);
  1089. *done = FALSE;
  1090. if(timeout_ms < 0) {
  1091. failf(data, "TFTP response timeout");
  1092. return CURLE_OPERATION_TIMEDOUT;
  1093. }
  1094. if(event != TFTP_EVENT_NONE) {
  1095. result = tftp_state_machine(state, event);
  1096. if(result)
  1097. return result;
  1098. *done = (state->state == TFTP_STATE_FIN) ? TRUE : FALSE;
  1099. if(*done)
  1100. /* Tell curl we're done */
  1101. Curl_setup_transfer(data, -1, -1, FALSE, -1);
  1102. }
  1103. else {
  1104. /* no timeouts to handle, check our socket */
  1105. int rc = SOCKET_READABLE(state->sockfd, 0);
  1106. if(rc == -1) {
  1107. /* bail out */
  1108. int error = SOCKERRNO;
  1109. char buffer[STRERROR_LEN];
  1110. failf(data, "%s", Curl_strerror(error, buffer, sizeof(buffer)));
  1111. state->event = TFTP_EVENT_ERROR;
  1112. }
  1113. else if(rc) {
  1114. result = tftp_receive_packet(data);
  1115. if(result)
  1116. return result;
  1117. result = tftp_state_machine(state, state->event);
  1118. if(result)
  1119. return result;
  1120. *done = (state->state == TFTP_STATE_FIN) ? TRUE : FALSE;
  1121. if(*done)
  1122. /* Tell curl we're done */
  1123. Curl_setup_transfer(data, -1, -1, FALSE, -1);
  1124. }
  1125. /* if rc == 0, then select() timed out */
  1126. }
  1127. return result;
  1128. }
  1129. /**********************************************************
  1130. *
  1131. * tftp_doing
  1132. *
  1133. * Called from multi.c while DOing
  1134. *
  1135. **********************************************************/
  1136. static CURLcode tftp_doing(struct Curl_easy *data, bool *dophase_done)
  1137. {
  1138. CURLcode result;
  1139. result = tftp_multi_statemach(data, dophase_done);
  1140. if(*dophase_done) {
  1141. DEBUGF(infof(data, "DO phase is complete"));
  1142. }
  1143. else if(!result) {
  1144. /* The multi code doesn't have this logic for the DOING state so we
  1145. provide it for TFTP since it may do the entire transfer in this
  1146. state. */
  1147. if(Curl_pgrsUpdate(data))
  1148. result = CURLE_ABORTED_BY_CALLBACK;
  1149. else
  1150. result = Curl_speedcheck(data, Curl_now());
  1151. }
  1152. return result;
  1153. }
  1154. /**********************************************************
  1155. *
  1156. * tftp_perform
  1157. *
  1158. * Entry point for transfer from tftp_do, starts state mach
  1159. *
  1160. **********************************************************/
  1161. static CURLcode tftp_perform(struct Curl_easy *data, bool *dophase_done)
  1162. {
  1163. CURLcode result = CURLE_OK;
  1164. struct connectdata *conn = data->conn;
  1165. struct tftp_state_data *state = conn->proto.tftpc;
  1166. *dophase_done = FALSE;
  1167. result = tftp_state_machine(state, TFTP_EVENT_INIT);
  1168. if((state->state == TFTP_STATE_FIN) || result)
  1169. return result;
  1170. tftp_multi_statemach(data, dophase_done);
  1171. if(*dophase_done)
  1172. DEBUGF(infof(data, "DO phase is complete"));
  1173. return result;
  1174. }
  1175. /**********************************************************
  1176. *
  1177. * tftp_do
  1178. *
  1179. * The do callback
  1180. *
  1181. * This callback initiates the TFTP transfer
  1182. *
  1183. **********************************************************/
  1184. static CURLcode tftp_do(struct Curl_easy *data, bool *done)
  1185. {
  1186. struct tftp_state_data *state;
  1187. CURLcode result;
  1188. struct connectdata *conn = data->conn;
  1189. *done = FALSE;
  1190. if(!conn->proto.tftpc) {
  1191. result = tftp_connect(data, done);
  1192. if(result)
  1193. return result;
  1194. }
  1195. state = conn->proto.tftpc;
  1196. if(!state)
  1197. return CURLE_TFTP_ILLEGAL;
  1198. result = tftp_perform(data, done);
  1199. /* If tftp_perform() returned an error, use that for return code. If it
  1200. was OK, see if tftp_translate_code() has an error. */
  1201. if(!result)
  1202. /* If we have encountered an internal tftp error, translate it. */
  1203. result = tftp_translate_code(state->error);
  1204. return result;
  1205. }
  1206. static CURLcode tftp_setup_connection(struct Curl_easy *data,
  1207. struct connectdata *conn)
  1208. {
  1209. char *type;
  1210. conn->transport = TRNSPRT_UDP;
  1211. /* TFTP URLs support an extension like ";mode=<typecode>" that
  1212. * we'll try to get now! */
  1213. type = strstr(data->state.up.path, ";mode=");
  1214. if(!type)
  1215. type = strstr(conn->host.rawalloc, ";mode=");
  1216. if(type) {
  1217. char command;
  1218. *type = 0; /* it was in the middle of the hostname */
  1219. command = Curl_raw_toupper(type[6]);
  1220. switch(command) {
  1221. case 'A': /* ASCII mode */
  1222. case 'N': /* NETASCII mode */
  1223. data->state.prefer_ascii = TRUE;
  1224. break;
  1225. case 'O': /* octet mode */
  1226. case 'I': /* binary mode */
  1227. default:
  1228. /* switch off ASCII */
  1229. data->state.prefer_ascii = FALSE;
  1230. break;
  1231. }
  1232. }
  1233. return CURLE_OK;
  1234. }
  1235. #endif