tftp.c 42 KB

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