tftp.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, 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 "cfilters.h"
  47. #include "cf-socket.h"
  48. #include "transfer.h"
  49. #include "sendf.h"
  50. #include "tftp.h"
  51. #include "progress.h"
  52. #include "connect.h"
  53. #include "sockaddr.h" /* required for Curl_sockaddr_storage */
  54. #include "multiif.h"
  55. #include "url.h"
  56. #include "strcase.h"
  57. #include "speedcheck.h"
  58. #include "select.h"
  59. #include "escape.h"
  60. #include "curlx/strerr.h"
  61. #include "curlx/strparse.h"
  62. /* The last 2 #include files should be in this order */
  63. #include "curl_memory.h"
  64. #include "memdebug.h"
  65. /* RFC2348 allows the block size to be negotiated */
  66. #define TFTP_BLKSIZE_DEFAULT 512
  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. /* meta key for storing protocol meta at connection */
  110. #define CURL_META_TFTP_CONN "meta:proto:tftp:conn"
  111. struct tftp_conn {
  112. struct Curl_sockaddr_storage local_addr;
  113. struct Curl_sockaddr_storage remote_addr;
  114. struct tftp_packet rpacket;
  115. struct tftp_packet spacket;
  116. tftp_state_t state;
  117. tftp_mode_t mode;
  118. tftp_error_t error;
  119. tftp_event_t event;
  120. struct Curl_easy *data;
  121. curl_socket_t sockfd;
  122. int retries;
  123. int retry_time;
  124. int retry_max;
  125. time_t rx_time;
  126. curl_socklen_t remote_addrlen;
  127. int rbytes;
  128. size_t sbytes;
  129. unsigned int blksize;
  130. unsigned int requested_blksize;
  131. unsigned short block;
  132. BIT(remote_pinned);
  133. };
  134. /* Forward declarations */
  135. static CURLcode tftp_rx(struct tftp_conn *state, tftp_event_t event);
  136. static CURLcode tftp_tx(struct tftp_conn *state, tftp_event_t event);
  137. static CURLcode tftp_connect(struct Curl_easy *data, bool *done);
  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 CURLcode tftp_pollset(struct Curl_easy *data,
  146. struct easy_pollset *ps);
  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_pollset, /* proto_pollset */
  161. tftp_pollset, /* doing_pollset */
  162. ZERO_NULL, /* domore_pollset */
  163. ZERO_NULL, /* perform_pollset */
  164. ZERO_NULL, /* disconnect */
  165. ZERO_NULL, /* write_resp */
  166. ZERO_NULL, /* write_resp_hd */
  167. ZERO_NULL, /* connection_check */
  168. ZERO_NULL, /* attach connection */
  169. ZERO_NULL, /* follow */
  170. PORT_TFTP, /* defport */
  171. CURLPROTO_TFTP, /* protocol */
  172. CURLPROTO_TFTP, /* family */
  173. PROTOPT_NOTCPPROXY | PROTOPT_NOURLQUERY /* flags */
  174. };
  175. /**********************************************************
  176. *
  177. * tftp_set_timeouts -
  178. *
  179. * Set timeouts based on state machine state.
  180. * Use user provided connect timeouts until DATA or ACK
  181. * packet is received, then use user-provided transfer timeouts
  182. *
  183. *
  184. **********************************************************/
  185. static CURLcode tftp_set_timeouts(struct tftp_conn *state)
  186. {
  187. time_t timeout;
  188. timediff_t timeout_ms;
  189. bool start = (state->state == TFTP_STATE_START);
  190. /* Compute drop-dead time */
  191. timeout_ms = Curl_timeleft(state->data, NULL, start);
  192. if(timeout_ms < 0) {
  193. /* time-out, bail out, go home */
  194. failf(state->data, "Connection time-out");
  195. return CURLE_OPERATION_TIMEDOUT;
  196. }
  197. /* Set per-block timeout to total */
  198. if(timeout_ms > 0)
  199. timeout = (time_t)(timeout_ms + 500) / 1000;
  200. else
  201. timeout = 15;
  202. /* Average reposting an ACK after 5 seconds */
  203. state->retry_max = (int)timeout/5;
  204. /* But bound the total number */
  205. if(state->retry_max < 3)
  206. state->retry_max = 3;
  207. if(state->retry_max > 50)
  208. state->retry_max = 50;
  209. /* Compute the re-ACK interval to suit the timeout */
  210. state->retry_time = (int)(timeout/state->retry_max);
  211. if(state->retry_time < 1)
  212. state->retry_time = 1;
  213. infof(state->data,
  214. "set timeouts for state %d; Total % " FMT_OFF_T ", retry %d maxtry %d",
  215. (int)state->state, timeout_ms, state->retry_time, state->retry_max);
  216. /* init RX time */
  217. state->rx_time = time(NULL);
  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_conn *state,
  267. const char *ptr, int len)
  268. {
  269. const char *tmp = ptr;
  270. struct Curl_easy *data = state->data;
  271. /* if OACK does not 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. curl_off_t blksize;
  283. if(curlx_str_number(&value, &blksize, TFTP_BLKSIZE_MAX)) {
  284. failf(data, "%s (%d)", "blksize is larger than max supported",
  285. TFTP_BLKSIZE_MAX);
  286. return CURLE_TFTP_ILLEGAL;
  287. }
  288. if(!blksize) {
  289. failf(data, "invalid blocksize value in OACK packet");
  290. return CURLE_TFTP_ILLEGAL;
  291. }
  292. else if(blksize < TFTP_BLKSIZE_MIN) {
  293. failf(data, "%s (%d)", "blksize is smaller than min supported",
  294. TFTP_BLKSIZE_MIN);
  295. return CURLE_TFTP_ILLEGAL;
  296. }
  297. else if(blksize > state->requested_blksize) {
  298. /* could realloc pkt buffers here, but the spec does not call out
  299. * support for the server requesting a bigger blksize than the client
  300. * requests */
  301. failf(data, "server requested blksize larger than allocated (%"
  302. CURL_FORMAT_CURL_OFF_T ")", blksize);
  303. return CURLE_TFTP_ILLEGAL;
  304. }
  305. state->blksize = (int)blksize;
  306. infof(data, "blksize parsed from OACK (%d) requested (%d)",
  307. state->blksize, state->requested_blksize);
  308. }
  309. else if(checkprefix(TFTP_OPTION_TSIZE, option)) {
  310. curl_off_t tsize = 0;
  311. /* tsize should be ignored on upload: Who cares about the size of the
  312. remote file? */
  313. if(!data->state.upload &&
  314. !curlx_str_number(&value, &tsize, CURL_OFF_T_MAX)) {
  315. if(!tsize) {
  316. failf(data, "invalid tsize -:%s:- value in OACK packet", value);
  317. return CURLE_TFTP_ILLEGAL;
  318. }
  319. infof(data, "tsize parsed from OACK (%" CURL_FORMAT_CURL_OFF_T ")",
  320. tsize);
  321. Curl_pgrsSetDownloadSize(data, tsize);
  322. }
  323. }
  324. }
  325. return CURLE_OK;
  326. }
  327. static CURLcode tftp_option_add(struct tftp_conn *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_conn *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_conn *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_conn *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. const struct Curl_sockaddr_ex *remote_addr = NULL;
  373. CURLcode result = CURLE_OK;
  374. /* Set ASCII mode if -B flag was used */
  375. if(data->state.prefer_ascii)
  376. mode = "netascii";
  377. switch(event) {
  378. case TFTP_EVENT_INIT: /* Send the first packet out */
  379. case TFTP_EVENT_TIMEOUT: /* Resend the first packet out */
  380. /* Increment the retry counter, quit if over the limit */
  381. state->retries++;
  382. if(state->retries > state->retry_max) {
  383. state->error = TFTP_ERR_NORESPONSE;
  384. state->state = TFTP_STATE_FIN;
  385. return result;
  386. }
  387. if(data->state.upload) {
  388. /* If we are uploading, send an WRQ */
  389. setpacketevent(&state->spacket, TFTP_EVENT_WRQ);
  390. if(data->state.infilesize != -1)
  391. Curl_pgrsSetUploadSize(data, data->state.infilesize);
  392. }
  393. else {
  394. /* If we are downloading, send an RRQ */
  395. setpacketevent(&state->spacket, TFTP_EVENT_RRQ);
  396. }
  397. /* As RFC3617 describes the separator slash is not actually part of the
  398. filename so we skip the always-present first letter of the path
  399. string. */
  400. if(!state->data->state.up.path[1]) {
  401. failf(data, "Missing filename");
  402. return CURLE_TFTP_ILLEGAL;
  403. }
  404. result = Curl_urldecode(&state->data->state.up.path[1], 0,
  405. &filename, NULL, REJECT_ZERO);
  406. if(result)
  407. return result;
  408. if(strlen(filename) > (state->blksize - strlen(mode) - 4)) {
  409. failf(data, "TFTP filename too long");
  410. free(filename);
  411. return CURLE_TFTP_ILLEGAL; /* too long filename field */
  412. }
  413. curl_msnprintf((char *)state->spacket.data + 2,
  414. state->blksize,
  415. "%s%c%s%c", filename, '\0', mode, '\0');
  416. sbytes = 4 + strlen(filename) + strlen(mode);
  417. /* optional addition of TFTP options */
  418. if(!data->set.tftp_no_options) {
  419. char buf[64];
  420. /* add tsize option */
  421. curl_msnprintf(buf, sizeof(buf), "%" FMT_OFF_T,
  422. data->state.upload && (data->state.infilesize != -1) ?
  423. data->state.infilesize : 0);
  424. result = tftp_option_add(state, &sbytes,
  425. (char *)state->spacket.data + sbytes,
  426. TFTP_OPTION_TSIZE);
  427. if(result == CURLE_OK)
  428. result = tftp_option_add(state, &sbytes,
  429. (char *)state->spacket.data + sbytes, buf);
  430. /* add blksize option */
  431. curl_msnprintf(buf, sizeof(buf), "%d", state->requested_blksize);
  432. if(result == CURLE_OK)
  433. result = tftp_option_add(state, &sbytes,
  434. (char *)state->spacket.data + sbytes,
  435. TFTP_OPTION_BLKSIZE);
  436. if(result == CURLE_OK)
  437. result = tftp_option_add(state, &sbytes,
  438. (char *)state->spacket.data + sbytes, buf);
  439. /* add timeout option */
  440. curl_msnprintf(buf, sizeof(buf), "%d", state->retry_time);
  441. if(result == CURLE_OK)
  442. result = tftp_option_add(state, &sbytes,
  443. (char *)state->spacket.data + sbytes,
  444. TFTP_OPTION_INTERVAL);
  445. if(result == CURLE_OK)
  446. result = tftp_option_add(state, &sbytes,
  447. (char *)state->spacket.data + sbytes, buf);
  448. if(result != CURLE_OK) {
  449. failf(data, "TFTP buffer too small for options");
  450. free(filename);
  451. return CURLE_TFTP_ILLEGAL;
  452. }
  453. }
  454. /* the typecase for the 3rd argument is mostly for systems that do
  455. not have a size_t argument, like older unixes that want an 'int' */
  456. #ifdef __AMIGA__
  457. #define CURL_SENDTO_ARG5(x) CURL_UNCONST(x)
  458. #else
  459. #define CURL_SENDTO_ARG5(x) (x)
  460. #endif
  461. remote_addr = Curl_conn_get_remote_addr(data, FIRSTSOCKET);
  462. if(!remote_addr)
  463. return CURLE_FAILED_INIT;
  464. senddata = sendto(state->sockfd, (void *)state->spacket.data,
  465. (SEND_TYPE_ARG3)sbytes, 0,
  466. CURL_SENDTO_ARG5(&remote_addr->curl_sa_addr),
  467. (curl_socklen_t)remote_addr->addrlen);
  468. free(filename);
  469. if(senddata != (ssize_t)sbytes) {
  470. char buffer[STRERROR_LEN];
  471. failf(data, "%s", curlx_strerror(SOCKERRNO, buffer, sizeof(buffer)));
  472. return CURLE_SEND_ERROR;
  473. }
  474. break;
  475. case TFTP_EVENT_OACK:
  476. if(data->state.upload) {
  477. result = tftp_connect_for_tx(state, event);
  478. }
  479. else {
  480. result = tftp_connect_for_rx(state, event);
  481. }
  482. break;
  483. case TFTP_EVENT_ACK: /* Connected for transmit */
  484. result = tftp_connect_for_tx(state, event);
  485. break;
  486. case TFTP_EVENT_DATA: /* Connected for receive */
  487. result = tftp_connect_for_rx(state, event);
  488. break;
  489. case TFTP_EVENT_ERROR:
  490. state->state = TFTP_STATE_FIN;
  491. break;
  492. default:
  493. failf(state->data, "tftp_send_first: internal error");
  494. return CURLE_TFTP_ILLEGAL;
  495. }
  496. return result;
  497. }
  498. /* the next blocknum is x + 1 but it needs to wrap at an unsigned 16bit
  499. boundary */
  500. #define NEXT_BLOCKNUM(x) (((x) + 1)&0xffff)
  501. /**********************************************************
  502. *
  503. * tftp_rx
  504. *
  505. * Event handler for the RX state
  506. *
  507. **********************************************************/
  508. static CURLcode tftp_rx(struct tftp_conn *state, tftp_event_t event)
  509. {
  510. ssize_t sbytes;
  511. int rblock;
  512. struct Curl_easy *data = state->data;
  513. char buffer[STRERROR_LEN];
  514. switch(event) {
  515. case TFTP_EVENT_DATA:
  516. /* Is this the block we expect? */
  517. rblock = getrpacketblock(&state->rpacket);
  518. if(NEXT_BLOCKNUM(state->block) == rblock) {
  519. /* This is the expected block. Reset counters and ACK it. */
  520. state->retries = 0;
  521. }
  522. else if(state->block == rblock) {
  523. /* This is the last recently received block again. Log it and ACK it
  524. again. */
  525. infof(data, "Received last DATA packet block %d again.", rblock);
  526. }
  527. else {
  528. /* totally unexpected, just log it */
  529. infof(data,
  530. "Received unexpected DATA packet block %d, expecting block %d",
  531. rblock, NEXT_BLOCKNUM(state->block));
  532. break;
  533. }
  534. /* ACK this block. */
  535. state->block = (unsigned short)rblock;
  536. setpacketevent(&state->spacket, TFTP_EVENT_ACK);
  537. setpacketblock(&state->spacket, state->block);
  538. sbytes = sendto(state->sockfd, (void *)state->spacket.data,
  539. 4, SEND_4TH_ARG,
  540. (struct sockaddr *)&state->remote_addr,
  541. state->remote_addrlen);
  542. if(sbytes < 0) {
  543. failf(data, "%s", curlx_strerror(SOCKERRNO, buffer, sizeof(buffer)));
  544. return CURLE_SEND_ERROR;
  545. }
  546. /* Check if completed (That is, a less than full packet is received) */
  547. if(state->rbytes < (ssize_t)state->blksize + 4) {
  548. state->state = TFTP_STATE_FIN;
  549. }
  550. else {
  551. state->state = TFTP_STATE_RX;
  552. }
  553. state->rx_time = time(NULL);
  554. break;
  555. case TFTP_EVENT_OACK:
  556. /* ACK option acknowledgement so we can move on to data */
  557. state->block = 0;
  558. state->retries = 0;
  559. setpacketevent(&state->spacket, TFTP_EVENT_ACK);
  560. setpacketblock(&state->spacket, state->block);
  561. sbytes = sendto(state->sockfd, (void *)state->spacket.data,
  562. 4, SEND_4TH_ARG,
  563. (struct sockaddr *)&state->remote_addr,
  564. state->remote_addrlen);
  565. if(sbytes < 0) {
  566. failf(data, "%s", curlx_strerror(SOCKERRNO, buffer, sizeof(buffer)));
  567. return CURLE_SEND_ERROR;
  568. }
  569. /* we are ready to RX data */
  570. state->state = TFTP_STATE_RX;
  571. state->rx_time = time(NULL);
  572. break;
  573. case TFTP_EVENT_TIMEOUT:
  574. /* Increment the retry count and fail if over the limit */
  575. state->retries++;
  576. infof(data,
  577. "Timeout waiting for block %d ACK. Retries = %d",
  578. NEXT_BLOCKNUM(state->block), state->retries);
  579. if(state->retries > state->retry_max) {
  580. state->error = TFTP_ERR_TIMEOUT;
  581. state->state = TFTP_STATE_FIN;
  582. }
  583. else {
  584. /* Resend the previous ACK */
  585. sbytes = sendto(state->sockfd, (void *)state->spacket.data,
  586. 4, SEND_4TH_ARG,
  587. (struct sockaddr *)&state->remote_addr,
  588. state->remote_addrlen);
  589. if(sbytes < 0) {
  590. failf(data, "%s", curlx_strerror(SOCKERRNO, buffer, sizeof(buffer)));
  591. return CURLE_SEND_ERROR;
  592. }
  593. }
  594. break;
  595. case TFTP_EVENT_ERROR:
  596. setpacketevent(&state->spacket, TFTP_EVENT_ERROR);
  597. setpacketblock(&state->spacket, state->block);
  598. (void)sendto(state->sockfd, (void *)state->spacket.data,
  599. 4, SEND_4TH_ARG,
  600. (struct sockaddr *)&state->remote_addr,
  601. state->remote_addrlen);
  602. /* do not bother with the return code, but if the socket is still up we
  603. * should be a good TFTP client and let the server know we are done */
  604. state->state = TFTP_STATE_FIN;
  605. break;
  606. default:
  607. failf(data, "%s", "tftp_rx: internal error");
  608. return CURLE_TFTP_ILLEGAL; /* not really the perfect return code for
  609. this */
  610. }
  611. return CURLE_OK;
  612. }
  613. /**********************************************************
  614. *
  615. * tftp_tx
  616. *
  617. * Event handler for the TX state
  618. *
  619. **********************************************************/
  620. static CURLcode tftp_tx(struct tftp_conn *state, tftp_event_t event)
  621. {
  622. struct Curl_easy *data = state->data;
  623. ssize_t sbytes;
  624. CURLcode result = CURLE_OK;
  625. struct SingleRequest *k = &data->req;
  626. size_t cb; /* Bytes currently read */
  627. char buffer[STRERROR_LEN];
  628. char *bufptr;
  629. bool eos;
  630. switch(event) {
  631. case TFTP_EVENT_ACK:
  632. case TFTP_EVENT_OACK:
  633. if(event == TFTP_EVENT_ACK) {
  634. /* Ack the packet */
  635. int rblock = getrpacketblock(&state->rpacket);
  636. if(rblock != state->block &&
  637. /* There is a bug in tftpd-hpa that causes it to send us an ack for
  638. * 65535 when the block number wraps to 0. So when we are expecting
  639. * 0, also accept 65535. See
  640. * https://www.syslinux.org/archives/2010-September/015612.html
  641. * */
  642. !(state->block == 0 && rblock == 65535)) {
  643. /* This is not the expected block. Log it and up the retry counter */
  644. infof(data, "Received ACK for block %d, expecting %d",
  645. rblock, state->block);
  646. state->retries++;
  647. /* Bail out if over the maximum */
  648. if(state->retries > state->retry_max) {
  649. failf(data, "tftp_tx: giving up waiting for block %d ack",
  650. state->block);
  651. result = CURLE_SEND_ERROR;
  652. }
  653. else {
  654. /* Re-send the data packet */
  655. sbytes = sendto(state->sockfd, (void *)state->spacket.data,
  656. 4 + (SEND_TYPE_ARG3)state->sbytes, SEND_4TH_ARG,
  657. (struct sockaddr *)&state->remote_addr,
  658. state->remote_addrlen);
  659. /* Check all sbytes were sent */
  660. if(sbytes < 0) {
  661. failf(data, "%s",
  662. curlx_strerror(SOCKERRNO, buffer, sizeof(buffer)));
  663. result = CURLE_SEND_ERROR;
  664. }
  665. }
  666. return result;
  667. }
  668. /* This is the expected packet. Reset the counters and send the next
  669. block */
  670. state->rx_time = time(NULL);
  671. state->block++;
  672. }
  673. else
  674. state->block = 1; /* first data block is 1 when using OACK */
  675. state->retries = 0;
  676. setpacketevent(&state->spacket, TFTP_EVENT_DATA);
  677. setpacketblock(&state->spacket, state->block);
  678. if(state->block > 1 && state->sbytes < state->blksize) {
  679. state->state = TFTP_STATE_FIN;
  680. return CURLE_OK;
  681. }
  682. /* TFTP considers data block size < 512 bytes as an end of session. So
  683. * in some cases we must wait for additional data to build full (512 bytes)
  684. * data block.
  685. * */
  686. state->sbytes = 0;
  687. bufptr = (char *)state->spacket.data + 4;
  688. do {
  689. result = Curl_client_read(data, bufptr, state->blksize - state->sbytes,
  690. &cb, &eos);
  691. if(result)
  692. return result;
  693. state->sbytes += cb;
  694. bufptr += cb;
  695. } while(state->sbytes < state->blksize && cb);
  696. sbytes = sendto(state->sockfd, (void *) state->spacket.data,
  697. 4 + (SEND_TYPE_ARG3)state->sbytes, SEND_4TH_ARG,
  698. (struct sockaddr *)&state->remote_addr,
  699. state->remote_addrlen);
  700. /* Check all sbytes were sent */
  701. if(sbytes < 0) {
  702. failf(data, "%s", curlx_strerror(SOCKERRNO, buffer, sizeof(buffer)));
  703. return CURLE_SEND_ERROR;
  704. }
  705. /* Update the progress meter */
  706. k->writebytecount += state->sbytes;
  707. Curl_pgrsSetUploadCounter(data, k->writebytecount);
  708. break;
  709. case TFTP_EVENT_TIMEOUT:
  710. /* Increment the retry counter and log the timeout */
  711. state->retries++;
  712. infof(data, "Timeout waiting for block %d ACK. "
  713. " Retries = %d", NEXT_BLOCKNUM(state->block), state->retries);
  714. /* Decide if we have had enough */
  715. if(state->retries > state->retry_max) {
  716. state->error = TFTP_ERR_TIMEOUT;
  717. state->state = TFTP_STATE_FIN;
  718. }
  719. else {
  720. /* Re-send the data packet */
  721. sbytes = sendto(state->sockfd, (void *)state->spacket.data,
  722. 4 + (SEND_TYPE_ARG3)state->sbytes, SEND_4TH_ARG,
  723. (struct sockaddr *)&state->remote_addr,
  724. state->remote_addrlen);
  725. /* Check all sbytes were sent */
  726. if(sbytes < 0) {
  727. failf(data, "%s", curlx_strerror(SOCKERRNO, buffer, sizeof(buffer)));
  728. return CURLE_SEND_ERROR;
  729. }
  730. /* since this was a re-send, we remain at the still byte position */
  731. Curl_pgrsSetUploadCounter(data, k->writebytecount);
  732. }
  733. break;
  734. case TFTP_EVENT_ERROR:
  735. state->state = TFTP_STATE_FIN;
  736. setpacketevent(&state->spacket, TFTP_EVENT_ERROR);
  737. setpacketblock(&state->spacket, state->block);
  738. (void)sendto(state->sockfd, (void *)state->spacket.data, 4, SEND_4TH_ARG,
  739. (struct sockaddr *)&state->remote_addr,
  740. state->remote_addrlen);
  741. /* do not bother with the return code, but if the socket is still up we
  742. * should be a good TFTP client and let the server know we are done */
  743. state->state = TFTP_STATE_FIN;
  744. break;
  745. default:
  746. failf(data, "tftp_tx: internal error, event: %i", (int)(event));
  747. break;
  748. }
  749. return result;
  750. }
  751. /**********************************************************
  752. *
  753. * tftp_translate_code
  754. *
  755. * Translate internal error codes to CURL error codes
  756. *
  757. **********************************************************/
  758. static CURLcode tftp_translate_code(tftp_error_t error)
  759. {
  760. CURLcode result = CURLE_OK;
  761. if(error != TFTP_ERR_NONE) {
  762. switch(error) {
  763. case TFTP_ERR_NOTFOUND:
  764. result = CURLE_TFTP_NOTFOUND;
  765. break;
  766. case TFTP_ERR_PERM:
  767. result = CURLE_TFTP_PERM;
  768. break;
  769. case TFTP_ERR_DISKFULL:
  770. result = CURLE_REMOTE_DISK_FULL;
  771. break;
  772. case TFTP_ERR_UNDEF:
  773. case TFTP_ERR_ILLEGAL:
  774. result = CURLE_TFTP_ILLEGAL;
  775. break;
  776. case TFTP_ERR_UNKNOWNID:
  777. result = CURLE_TFTP_UNKNOWNID;
  778. break;
  779. case TFTP_ERR_EXISTS:
  780. result = CURLE_REMOTE_FILE_EXISTS;
  781. break;
  782. case TFTP_ERR_NOSUCHUSER:
  783. result = CURLE_TFTP_NOSUCHUSER;
  784. break;
  785. case TFTP_ERR_TIMEOUT:
  786. result = CURLE_OPERATION_TIMEDOUT;
  787. break;
  788. case TFTP_ERR_NORESPONSE:
  789. result = CURLE_COULDNT_CONNECT;
  790. break;
  791. default:
  792. result = CURLE_ABORTED_BY_CALLBACK;
  793. break;
  794. }
  795. }
  796. else
  797. result = CURLE_OK;
  798. return result;
  799. }
  800. /**********************************************************
  801. *
  802. * tftp_state_machine
  803. *
  804. * The tftp state machine event dispatcher
  805. *
  806. **********************************************************/
  807. static CURLcode tftp_state_machine(struct tftp_conn *state,
  808. tftp_event_t event)
  809. {
  810. CURLcode result = CURLE_OK;
  811. struct Curl_easy *data = state->data;
  812. switch(state->state) {
  813. case TFTP_STATE_START:
  814. DEBUGF(infof(data, "TFTP_STATE_START"));
  815. result = tftp_send_first(state, event);
  816. break;
  817. case TFTP_STATE_RX:
  818. DEBUGF(infof(data, "TFTP_STATE_RX"));
  819. result = tftp_rx(state, event);
  820. break;
  821. case TFTP_STATE_TX:
  822. DEBUGF(infof(data, "TFTP_STATE_TX"));
  823. result = tftp_tx(state, event);
  824. break;
  825. case TFTP_STATE_FIN:
  826. infof(data, "%s", "TFTP finished");
  827. break;
  828. default:
  829. DEBUGF(infof(data, "STATE: %d", state->state));
  830. failf(data, "%s", "Internal state machine error");
  831. result = CURLE_TFTP_ILLEGAL;
  832. break;
  833. }
  834. return result;
  835. }
  836. static void tftp_conn_dtor(void *key, size_t klen, void *entry)
  837. {
  838. struct tftp_conn *state = entry;
  839. (void)key;
  840. (void)klen;
  841. Curl_safefree(state->rpacket.data);
  842. Curl_safefree(state->spacket.data);
  843. free(state);
  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_conn *state;
  855. int blksize;
  856. int need_blksize;
  857. struct connectdata *conn = data->conn;
  858. const struct Curl_sockaddr_ex *remote_addr = NULL;
  859. CURLcode result;
  860. blksize = TFTP_BLKSIZE_DEFAULT;
  861. state = calloc(1, sizeof(*state));
  862. if(!state ||
  863. Curl_conn_meta_set(conn, CURL_META_TFTP_CONN, state, tftp_conn_dtor))
  864. return CURLE_OUT_OF_MEMORY;
  865. /* alloc pkt buffers based on specified blksize */
  866. if(data->set.tftp_blksize)
  867. /* range checked when set */
  868. blksize = (int)data->set.tftp_blksize;
  869. need_blksize = blksize;
  870. /* default size is the fallback when no OACK is received */
  871. if(need_blksize < TFTP_BLKSIZE_DEFAULT)
  872. need_blksize = TFTP_BLKSIZE_DEFAULT;
  873. if(!state->rpacket.data) {
  874. state->rpacket.data = calloc(1, need_blksize + 2 + 2);
  875. if(!state->rpacket.data)
  876. return CURLE_OUT_OF_MEMORY;
  877. }
  878. if(!state->spacket.data) {
  879. state->spacket.data = calloc(1, need_blksize + 2 + 2);
  880. if(!state->spacket.data)
  881. return CURLE_OUT_OF_MEMORY;
  882. }
  883. /* we do not keep TFTP connections up basically because there is none or
  884. * little gain for UDP */
  885. connclose(conn, "TFTP");
  886. state->data = data;
  887. state->sockfd = conn->sock[FIRSTSOCKET];
  888. state->state = TFTP_STATE_START;
  889. state->error = TFTP_ERR_NONE;
  890. state->blksize = TFTP_BLKSIZE_DEFAULT; /* Unless updated by OACK response */
  891. state->requested_blksize = blksize;
  892. remote_addr = Curl_conn_get_remote_addr(data, FIRSTSOCKET);
  893. DEBUGASSERT(remote_addr);
  894. if(!remote_addr)
  895. return CURLE_FAILED_INIT;
  896. ((struct sockaddr *)&state->local_addr)->sa_family =
  897. (CURL_SA_FAMILY_T)(remote_addr->family);
  898. result = tftp_set_timeouts(state);
  899. if(result)
  900. return result;
  901. if(!conn->bits.bound) {
  902. /* If not already bound, bind to any interface, random UDP port. If it is
  903. * reused or a custom local port was desired, this has already been done!
  904. *
  905. * We once used the size of the local_addr struct as the third argument
  906. * for bind() to better work with IPv6 or whatever size the struct could
  907. * have, but we learned that at least Tru64, AIX and IRIX *requires* the
  908. * size of that argument to match the exact size of a 'sockaddr_in' struct
  909. * when running IPv4-only.
  910. *
  911. * Therefore we use the size from the address we connected to, which we
  912. * assume uses the same IP version and thus hopefully this works for both
  913. * IPv4 and IPv6...
  914. */
  915. int rc = bind(state->sockfd, (struct sockaddr *)&state->local_addr,
  916. (curl_socklen_t)remote_addr->addrlen);
  917. if(rc) {
  918. char buffer[STRERROR_LEN];
  919. failf(data, "bind() failed; %s",
  920. curlx_strerror(SOCKERRNO, buffer, sizeof(buffer)));
  921. return CURLE_COULDNT_CONNECT;
  922. }
  923. conn->bits.bound = TRUE;
  924. }
  925. Curl_pgrsStartNow(data);
  926. *done = TRUE;
  927. return CURLE_OK;
  928. }
  929. /**********************************************************
  930. *
  931. * tftp_done
  932. *
  933. * The done callback
  934. *
  935. **********************************************************/
  936. static CURLcode tftp_done(struct Curl_easy *data, CURLcode status,
  937. bool premature)
  938. {
  939. CURLcode result = CURLE_OK;
  940. struct connectdata *conn = data->conn;
  941. struct tftp_conn *state = Curl_conn_meta_get(conn, CURL_META_TFTP_CONN);
  942. (void)status;
  943. (void)premature;
  944. if(Curl_pgrsDone(data))
  945. return CURLE_ABORTED_BY_CALLBACK;
  946. /* If we have encountered an error */
  947. if(state)
  948. result = tftp_translate_code(state->error);
  949. return result;
  950. }
  951. static CURLcode tftp_pollset(struct Curl_easy *data,
  952. struct easy_pollset *ps)
  953. {
  954. return Curl_pollset_add_in(data, ps, data->conn->sock[FIRSTSOCKET]);
  955. }
  956. /**********************************************************
  957. *
  958. * tftp_receive_packet
  959. *
  960. * Called once select fires and data is ready on the socket
  961. *
  962. **********************************************************/
  963. static CURLcode tftp_receive_packet(struct Curl_easy *data,
  964. struct tftp_conn *state)
  965. {
  966. CURLcode result = CURLE_OK;
  967. struct Curl_sockaddr_storage remote_addr;
  968. curl_socklen_t fromlen = sizeof(remote_addr);
  969. /* Receive the packet */
  970. state->rbytes = (int)recvfrom(state->sockfd,
  971. (void *)state->rpacket.data,
  972. (RECV_TYPE_ARG3)state->blksize + 4,
  973. 0,
  974. (struct sockaddr *)&remote_addr,
  975. &fromlen);
  976. if((state->rbytes >= 0) && fromlen) {
  977. if(state->remote_pinned) {
  978. /* pinned, verify that it comes from the same address */
  979. if((state->remote_addrlen != fromlen) ||
  980. memcmp(&remote_addr, &state->remote_addr, fromlen)) {
  981. failf(data, "Data received from another address");
  982. return CURLE_RECV_ERROR;
  983. }
  984. }
  985. else {
  986. /* pin address on first use */
  987. state->remote_pinned = TRUE;
  988. state->remote_addrlen = fromlen;
  989. memcpy(&state->remote_addr, &remote_addr, fromlen);
  990. }
  991. }
  992. /* Sanity check packet length */
  993. if(state->rbytes < 4) {
  994. failf(data, "Received too short packet");
  995. /* Not a timeout, but how best to handle it? */
  996. state->event = TFTP_EVENT_TIMEOUT;
  997. }
  998. else {
  999. /* The event is given by the TFTP packet time */
  1000. unsigned short event = getrpacketevent(&state->rpacket);
  1001. state->event = (tftp_event_t)event;
  1002. switch(state->event) {
  1003. case TFTP_EVENT_DATA:
  1004. /* Do not pass to the client empty or retransmitted packets */
  1005. if(state->rbytes > 4 &&
  1006. (NEXT_BLOCKNUM(state->block) == getrpacketblock(&state->rpacket))) {
  1007. result = Curl_client_write(data, CLIENTWRITE_BODY,
  1008. (char *)state->rpacket.data + 4,
  1009. state->rbytes-4);
  1010. if(result) {
  1011. tftp_state_machine(state, TFTP_EVENT_ERROR);
  1012. return result;
  1013. }
  1014. }
  1015. break;
  1016. case TFTP_EVENT_ERROR:
  1017. {
  1018. unsigned short error = getrpacketblock(&state->rpacket);
  1019. char *str = (char *)state->rpacket.data + 4;
  1020. size_t strn = state->rbytes - 4;
  1021. state->error = (tftp_error_t)error;
  1022. if(tftp_strnlen(str, strn) < strn)
  1023. infof(data, "TFTP error: %s", str);
  1024. break;
  1025. }
  1026. case TFTP_EVENT_ACK:
  1027. break;
  1028. case TFTP_EVENT_OACK:
  1029. result = tftp_parse_option_ack(state,
  1030. (const char *)state->rpacket.data + 2,
  1031. state->rbytes-2);
  1032. if(result)
  1033. return result;
  1034. break;
  1035. case TFTP_EVENT_RRQ:
  1036. case TFTP_EVENT_WRQ:
  1037. default:
  1038. failf(data, "%s", "Internal error: Unexpected packet");
  1039. break;
  1040. }
  1041. /* Update the progress meter */
  1042. if(Curl_pgrsUpdate(data)) {
  1043. tftp_state_machine(state, TFTP_EVENT_ERROR);
  1044. return CURLE_ABORTED_BY_CALLBACK;
  1045. }
  1046. }
  1047. return result;
  1048. }
  1049. /**********************************************************
  1050. *
  1051. * tftp_state_timeout
  1052. *
  1053. * Check if timeouts have been reached
  1054. *
  1055. **********************************************************/
  1056. static timediff_t tftp_state_timeout(struct tftp_conn *state,
  1057. tftp_event_t *event)
  1058. {
  1059. time_t current;
  1060. timediff_t timeout_ms;
  1061. if(event)
  1062. *event = TFTP_EVENT_NONE;
  1063. timeout_ms = Curl_timeleft(state->data, NULL,
  1064. (state->state == TFTP_STATE_START));
  1065. if(timeout_ms < 0) {
  1066. state->error = TFTP_ERR_TIMEOUT;
  1067. state->state = TFTP_STATE_FIN;
  1068. return timeout_ms;
  1069. }
  1070. current = time(NULL);
  1071. if(current > state->rx_time + state->retry_time) {
  1072. if(event)
  1073. *event = TFTP_EVENT_TIMEOUT;
  1074. state->rx_time = time(NULL); /* update even though we received nothing */
  1075. }
  1076. return timeout_ms;
  1077. }
  1078. /**********************************************************
  1079. *
  1080. * tftp_multi_statemach
  1081. *
  1082. * Handle single RX socket event and return
  1083. *
  1084. **********************************************************/
  1085. static CURLcode tftp_multi_statemach(struct Curl_easy *data, bool *done)
  1086. {
  1087. tftp_event_t event;
  1088. CURLcode result = CURLE_OK;
  1089. struct connectdata *conn = data->conn;
  1090. struct tftp_conn *state = Curl_conn_meta_get(conn, CURL_META_TFTP_CONN);
  1091. timediff_t timeout_ms;
  1092. *done = FALSE;
  1093. if(!state)
  1094. return CURLE_FAILED_INIT;
  1095. timeout_ms = tftp_state_timeout(state, &event);
  1096. if(timeout_ms < 0) {
  1097. failf(data, "TFTP response timeout");
  1098. return CURLE_OPERATION_TIMEDOUT;
  1099. }
  1100. if(event != TFTP_EVENT_NONE) {
  1101. result = tftp_state_machine(state, event);
  1102. if(result)
  1103. return result;
  1104. *done = (state->state == TFTP_STATE_FIN);
  1105. if(*done)
  1106. /* Tell curl we are done */
  1107. Curl_xfer_setup_nop(data);
  1108. }
  1109. else {
  1110. /* no timeouts to handle, check our socket */
  1111. int rc = SOCKET_READABLE(state->sockfd, 0);
  1112. if(rc == -1) {
  1113. /* bail out */
  1114. int error = SOCKERRNO;
  1115. char buffer[STRERROR_LEN];
  1116. failf(data, "%s", curlx_strerror(error, buffer, sizeof(buffer)));
  1117. state->event = TFTP_EVENT_ERROR;
  1118. }
  1119. else if(rc) {
  1120. result = tftp_receive_packet(data, state);
  1121. if(result)
  1122. return result;
  1123. result = tftp_state_machine(state, state->event);
  1124. if(result)
  1125. return result;
  1126. *done = (state->state == TFTP_STATE_FIN);
  1127. if(*done)
  1128. /* Tell curl we are done */
  1129. Curl_xfer_setup_nop(data);
  1130. }
  1131. /* if rc == 0, then select() timed out */
  1132. }
  1133. return result;
  1134. }
  1135. /**********************************************************
  1136. *
  1137. * tftp_doing
  1138. *
  1139. * Called from multi.c while DOing
  1140. *
  1141. **********************************************************/
  1142. static CURLcode tftp_doing(struct Curl_easy *data, bool *dophase_done)
  1143. {
  1144. CURLcode result;
  1145. result = tftp_multi_statemach(data, dophase_done);
  1146. if(*dophase_done) {
  1147. DEBUGF(infof(data, "DO phase is complete"));
  1148. }
  1149. else if(!result) {
  1150. /* The multi code does not have this logic for the DOING state so we
  1151. provide it for TFTP since it may do the entire transfer in this
  1152. state. */
  1153. if(Curl_pgrsUpdate(data))
  1154. result = CURLE_ABORTED_BY_CALLBACK;
  1155. else
  1156. result = Curl_speedcheck(data, curlx_now());
  1157. }
  1158. return result;
  1159. }
  1160. /**********************************************************
  1161. *
  1162. * tftp_perform
  1163. *
  1164. * Entry point for transfer from tftp_do, starts state mach
  1165. *
  1166. **********************************************************/
  1167. static CURLcode tftp_perform(struct Curl_easy *data, bool *dophase_done)
  1168. {
  1169. CURLcode result = CURLE_OK;
  1170. struct connectdata *conn = data->conn;
  1171. struct tftp_conn *state = Curl_conn_meta_get(conn, CURL_META_TFTP_CONN);
  1172. *dophase_done = FALSE;
  1173. if(!state)
  1174. return CURLE_FAILED_INIT;
  1175. result = tftp_state_machine(state, TFTP_EVENT_INIT);
  1176. if((state->state == TFTP_STATE_FIN) || result)
  1177. return result;
  1178. result = tftp_multi_statemach(data, dophase_done);
  1179. if(*dophase_done)
  1180. DEBUGF(infof(data, "DO phase is complete"));
  1181. return result;
  1182. }
  1183. /**********************************************************
  1184. *
  1185. * tftp_do
  1186. *
  1187. * The do callback
  1188. *
  1189. * This callback initiates the TFTP transfer
  1190. *
  1191. **********************************************************/
  1192. static CURLcode tftp_do(struct Curl_easy *data, bool *done)
  1193. {
  1194. struct connectdata *conn = data->conn;
  1195. struct tftp_conn *state = Curl_conn_meta_get(conn, CURL_META_TFTP_CONN);
  1196. CURLcode result;
  1197. *done = FALSE;
  1198. if(!state) {
  1199. result = tftp_connect(data, done);
  1200. if(result)
  1201. return result;
  1202. state = Curl_conn_meta_get(conn, CURL_META_TFTP_CONN);
  1203. if(!state)
  1204. return CURLE_TFTP_ILLEGAL;
  1205. }
  1206. result = tftp_perform(data, done);
  1207. /* If tftp_perform() returned an error, use that for return code. If it
  1208. was OK, see if tftp_translate_code() has an error. */
  1209. if(!result)
  1210. /* If we have encountered an internal tftp error, translate it. */
  1211. result = tftp_translate_code(state->error);
  1212. return result;
  1213. }
  1214. static CURLcode tftp_setup_connection(struct Curl_easy *data,
  1215. struct connectdata *conn)
  1216. {
  1217. char *path = data->state.up.path;
  1218. size_t len = strlen(path);
  1219. conn->transport_wanted = TRNSPRT_UDP;
  1220. /* TFTP URLs support a trailing ";mode=netascii" or ";mode=octet" */
  1221. if((len >= 14) && !memcmp(&path[len - 14], ";mode=netascii", 14)) {
  1222. data->state.prefer_ascii = TRUE;
  1223. path[len - 14] = 0; /* cut it there */
  1224. }
  1225. else if((len >= 11) && !memcmp(&path[len - 11], ";mode=octet", 11)) {
  1226. data->state.prefer_ascii = FALSE;
  1227. path[len - 11] = 0; /* cut it there */
  1228. }
  1229. return CURLE_OK;
  1230. }
  1231. #endif