tftp.c 41 KB

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