tftp.c 41 KB

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