tftp.c 41 KB

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