tftp.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2016, Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.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. 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(tftp_state_data_t *state)
  179. {
  180. time_t maxtime, timeout;
  181. time_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(tftp_packet_t *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(tftp_packet_t *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 tftp_packet_t *packet)
  252. {
  253. return (unsigned short)((packet->data[0] << 8) | packet->data[1]);
  254. }
  255. static unsigned short getrpacketblock(const tftp_packet_t *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(tftp_state_data_t *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. else 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 size_t tftp_option_add(tftp_state_data_t *state, size_t csize,
  342. char *buf, const char *option)
  343. {
  344. if(( strlen(option) + csize + 1) > (size_t)state->blksize)
  345. return 0;
  346. strcpy(buf, option);
  347. return strlen(option) + 1;
  348. }
  349. static CURLcode tftp_connect_for_tx(tftp_state_data_t *state,
  350. tftp_event_t event)
  351. {
  352. CURLcode result;
  353. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  354. struct Curl_easy *data = state->conn->data;
  355. infof(data, "%s\n", "Connected for transmit");
  356. #endif
  357. state->state = TFTP_STATE_TX;
  358. result = tftp_set_timeouts(state);
  359. if(result)
  360. return result;
  361. return tftp_tx(state, event);
  362. }
  363. static CURLcode tftp_connect_for_rx(tftp_state_data_t *state,
  364. tftp_event_t event)
  365. {
  366. CURLcode result;
  367. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  368. struct Curl_easy *data = state->conn->data;
  369. infof(data, "%s\n", "Connected for receive");
  370. #endif
  371. state->state = TFTP_STATE_RX;
  372. result = tftp_set_timeouts(state);
  373. if(result)
  374. return result;
  375. return tftp_rx(state, event);
  376. }
  377. static CURLcode tftp_send_first(tftp_state_data_t *state, tftp_event_t event)
  378. {
  379. size_t sbytes;
  380. ssize_t senddata;
  381. const char *mode = "octet";
  382. char *filename;
  383. char buf[64];
  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. snprintf((char *)state->spacket.data+2,
  419. state->blksize,
  420. "%s%c%s%c", filename, '\0', mode, '\0');
  421. sbytes = 4 + strlen(filename) + strlen(mode);
  422. /* optional addition of TFTP options */
  423. if(!data->set.tftp_no_options) {
  424. /* add tsize option */
  425. if(data->set.upload && (data->state.infilesize != -1))
  426. snprintf(buf, sizeof(buf), "%" CURL_FORMAT_CURL_OFF_T,
  427. data->state.infilesize);
  428. else
  429. strcpy(buf, "0"); /* the destination is large enough */
  430. sbytes += tftp_option_add(state, sbytes,
  431. (char *)state->spacket.data+sbytes,
  432. TFTP_OPTION_TSIZE);
  433. sbytes += tftp_option_add(state, sbytes,
  434. (char *)state->spacket.data+sbytes, buf);
  435. /* add blksize option */
  436. snprintf(buf, sizeof(buf), "%d", state->requested_blksize);
  437. sbytes += tftp_option_add(state, sbytes,
  438. (char *)state->spacket.data+sbytes,
  439. TFTP_OPTION_BLKSIZE);
  440. sbytes += tftp_option_add(state, sbytes,
  441. (char *)state->spacket.data+sbytes, buf);
  442. /* add timeout option */
  443. snprintf(buf, sizeof(buf), "%d", state->retry_time);
  444. sbytes += tftp_option_add(state, sbytes,
  445. (char *)state->spacket.data+sbytes,
  446. TFTP_OPTION_INTERVAL);
  447. sbytes += tftp_option_add(state, sbytes,
  448. (char *)state->spacket.data+sbytes, buf);
  449. }
  450. /* the typecase for the 3rd argument is mostly for systems that do
  451. not have a size_t argument, like older unixes that want an 'int' */
  452. senddata = sendto(state->sockfd, (void *)state->spacket.data,
  453. (SEND_TYPE_ARG3)sbytes, 0,
  454. state->conn->ip_addr->ai_addr,
  455. state->conn->ip_addr->ai_addrlen);
  456. if(senddata != (ssize_t)sbytes) {
  457. failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO));
  458. }
  459. free(filename);
  460. break;
  461. case TFTP_EVENT_OACK:
  462. if(data->set.upload) {
  463. result = tftp_connect_for_tx(state, event);
  464. }
  465. else {
  466. result = tftp_connect_for_rx(state, event);
  467. }
  468. break;
  469. case TFTP_EVENT_ACK: /* Connected for transmit */
  470. result = tftp_connect_for_tx(state, event);
  471. break;
  472. case TFTP_EVENT_DATA: /* Connected for receive */
  473. result = tftp_connect_for_rx(state, event);
  474. break;
  475. case TFTP_EVENT_ERROR:
  476. state->state = TFTP_STATE_FIN;
  477. break;
  478. default:
  479. failf(state->conn->data, "tftp_send_first: internal error");
  480. break;
  481. }
  482. return result;
  483. }
  484. /* the next blocknum is x + 1 but it needs to wrap at an unsigned 16bit
  485. boundary */
  486. #define NEXT_BLOCKNUM(x) (((x)+1)&0xffff)
  487. /**********************************************************
  488. *
  489. * tftp_rx
  490. *
  491. * Event handler for the RX state
  492. *
  493. **********************************************************/
  494. static CURLcode tftp_rx(tftp_state_data_t *state, tftp_event_t event)
  495. {
  496. ssize_t sbytes;
  497. int rblock;
  498. struct Curl_easy *data = state->conn->data;
  499. switch(event) {
  500. case TFTP_EVENT_DATA:
  501. /* Is this the block we expect? */
  502. rblock = getrpacketblock(&state->rpacket);
  503. if(NEXT_BLOCKNUM(state->block) == rblock) {
  504. /* This is the expected block. Reset counters and ACK it. */
  505. state->retries = 0;
  506. }
  507. else if(state->block == rblock) {
  508. /* This is the last recently received block again. Log it and ACK it
  509. again. */
  510. infof(data, "Received last DATA packet block %d again.\n", rblock);
  511. }
  512. else {
  513. /* totally unexpected, just log it */
  514. infof(data,
  515. "Received unexpected DATA packet block %d, expecting block %d\n",
  516. rblock, NEXT_BLOCKNUM(state->block));
  517. break;
  518. }
  519. /* ACK this block. */
  520. state->block = (unsigned short)rblock;
  521. setpacketevent(&state->spacket, TFTP_EVENT_ACK);
  522. setpacketblock(&state->spacket, state->block);
  523. sbytes = sendto(state->sockfd, (void *)state->spacket.data,
  524. 4, SEND_4TH_ARG,
  525. (struct sockaddr *)&state->remote_addr,
  526. state->remote_addrlen);
  527. if(sbytes < 0) {
  528. failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO));
  529. return CURLE_SEND_ERROR;
  530. }
  531. /* Check if completed (That is, a less than full packet is received) */
  532. if(state->rbytes < (ssize_t)state->blksize+4) {
  533. state->state = TFTP_STATE_FIN;
  534. }
  535. else {
  536. state->state = TFTP_STATE_RX;
  537. }
  538. time(&state->rx_time);
  539. break;
  540. case TFTP_EVENT_OACK:
  541. /* ACK option acknowledgement so we can move on to data */
  542. state->block = 0;
  543. state->retries = 0;
  544. setpacketevent(&state->spacket, TFTP_EVENT_ACK);
  545. setpacketblock(&state->spacket, state->block);
  546. sbytes = sendto(state->sockfd, (void *)state->spacket.data,
  547. 4, SEND_4TH_ARG,
  548. (struct sockaddr *)&state->remote_addr,
  549. state->remote_addrlen);
  550. if(sbytes < 0) {
  551. failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO));
  552. return CURLE_SEND_ERROR;
  553. }
  554. /* we're ready to RX data */
  555. state->state = TFTP_STATE_RX;
  556. time(&state->rx_time);
  557. break;
  558. case TFTP_EVENT_TIMEOUT:
  559. /* Increment the retry count and fail if over the limit */
  560. state->retries++;
  561. infof(data,
  562. "Timeout waiting for block %d ACK. Retries = %d\n",
  563. NEXT_BLOCKNUM(state->block), state->retries);
  564. if(state->retries > state->retry_max) {
  565. state->error = TFTP_ERR_TIMEOUT;
  566. state->state = TFTP_STATE_FIN;
  567. }
  568. else {
  569. /* Resend the previous ACK */
  570. sbytes = sendto(state->sockfd, (void *)state->spacket.data,
  571. 4, SEND_4TH_ARG,
  572. (struct sockaddr *)&state->remote_addr,
  573. state->remote_addrlen);
  574. if(sbytes<0) {
  575. failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO));
  576. return CURLE_SEND_ERROR;
  577. }
  578. }
  579. break;
  580. case TFTP_EVENT_ERROR:
  581. setpacketevent(&state->spacket, TFTP_EVENT_ERROR);
  582. setpacketblock(&state->spacket, state->block);
  583. (void)sendto(state->sockfd, (void *)state->spacket.data,
  584. 4, SEND_4TH_ARG,
  585. (struct sockaddr *)&state->remote_addr,
  586. state->remote_addrlen);
  587. /* don't bother with the return code, but if the socket is still up we
  588. * should be a good TFTP client and let the server know we're done */
  589. state->state = TFTP_STATE_FIN;
  590. break;
  591. default:
  592. failf(data, "%s", "tftp_rx: internal error");
  593. return CURLE_TFTP_ILLEGAL; /* not really the perfect return code for
  594. this */
  595. }
  596. return CURLE_OK;
  597. }
  598. /**********************************************************
  599. *
  600. * tftp_tx
  601. *
  602. * Event handler for the TX state
  603. *
  604. **********************************************************/
  605. static CURLcode tftp_tx(tftp_state_data_t *state, tftp_event_t event)
  606. {
  607. struct Curl_easy *data = state->conn->data;
  608. ssize_t sbytes;
  609. int rblock;
  610. CURLcode result = CURLE_OK;
  611. struct SingleRequest *k = &data->req;
  612. int cb; /* Bytes currently read */
  613. switch(event) {
  614. case TFTP_EVENT_ACK:
  615. case TFTP_EVENT_OACK:
  616. if(event == TFTP_EVENT_ACK) {
  617. /* Ack the packet */
  618. rblock = getrpacketblock(&state->rpacket);
  619. if(rblock != state->block &&
  620. /* There's a bug in tftpd-hpa that causes it to send us an ack for
  621. * 65535 when the block number wraps to 0. So when we're expecting
  622. * 0, also accept 65535. See
  623. * http://syslinux.zytor.com/archives/2010-September/015253.html
  624. * */
  625. !(state->block == 0 && rblock == 65535)) {
  626. /* This isn't the expected block. Log it and up the retry counter */
  627. infof(data, "Received ACK for block %d, expecting %d\n",
  628. rblock, state->block);
  629. state->retries++;
  630. /* Bail out if over the maximum */
  631. if(state->retries>state->retry_max) {
  632. failf(data, "tftp_tx: giving up waiting for block %d ack",
  633. state->block);
  634. result = CURLE_SEND_ERROR;
  635. }
  636. else {
  637. /* Re-send the data packet */
  638. sbytes = sendto(state->sockfd, (void *)state->spacket.data,
  639. 4+state->sbytes, SEND_4TH_ARG,
  640. (struct sockaddr *)&state->remote_addr,
  641. state->remote_addrlen);
  642. /* Check all sbytes were sent */
  643. if(sbytes<0) {
  644. failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO));
  645. result = CURLE_SEND_ERROR;
  646. }
  647. }
  648. return result;
  649. }
  650. /* This is the expected packet. Reset the counters and send the next
  651. block */
  652. time(&state->rx_time);
  653. state->block++;
  654. }
  655. else
  656. state->block = 1; /* first data block is 1 when using OACK */
  657. state->retries = 0;
  658. setpacketevent(&state->spacket, TFTP_EVENT_DATA);
  659. setpacketblock(&state->spacket, state->block);
  660. if(state->block > 1 && state->sbytes < (int)state->blksize) {
  661. state->state = TFTP_STATE_FIN;
  662. return CURLE_OK;
  663. }
  664. /* TFTP considers data block size < 512 bytes as an end of session. So
  665. * in some cases we must wait for additional data to build full (512 bytes)
  666. * data block.
  667. * */
  668. state->sbytes = 0;
  669. state->conn->data->req.upload_fromhere = (char *)state->spacket.data+4;
  670. do {
  671. result = Curl_fillreadbuffer(state->conn, state->blksize - state->sbytes,
  672. &cb);
  673. if(result)
  674. return result;
  675. state->sbytes += cb;
  676. state->conn->data->req.upload_fromhere += cb;
  677. } while(state->sbytes < state->blksize && cb != 0);
  678. sbytes = sendto(state->sockfd, (void *) state->spacket.data,
  679. 4 + state->sbytes, SEND_4TH_ARG,
  680. (struct sockaddr *)&state->remote_addr,
  681. state->remote_addrlen);
  682. /* Check all sbytes were sent */
  683. if(sbytes<0) {
  684. failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO));
  685. return CURLE_SEND_ERROR;
  686. }
  687. /* Update the progress meter */
  688. k->writebytecount += state->sbytes;
  689. Curl_pgrsSetUploadCounter(data, k->writebytecount);
  690. break;
  691. case TFTP_EVENT_TIMEOUT:
  692. /* Increment the retry counter and log the timeout */
  693. state->retries++;
  694. infof(data, "Timeout waiting for block %d ACK. "
  695. " Retries = %d\n", NEXT_BLOCKNUM(state->block), state->retries);
  696. /* Decide if we've had enough */
  697. if(state->retries > state->retry_max) {
  698. state->error = TFTP_ERR_TIMEOUT;
  699. state->state = TFTP_STATE_FIN;
  700. }
  701. else {
  702. /* Re-send the data packet */
  703. sbytes = sendto(state->sockfd, (void *)state->spacket.data,
  704. 4+state->sbytes, SEND_4TH_ARG,
  705. (struct sockaddr *)&state->remote_addr,
  706. state->remote_addrlen);
  707. /* Check all sbytes were sent */
  708. if(sbytes<0) {
  709. failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO));
  710. return CURLE_SEND_ERROR;
  711. }
  712. /* since this was a re-send, we remain at the still byte position */
  713. Curl_pgrsSetUploadCounter(data, k->writebytecount);
  714. }
  715. break;
  716. case TFTP_EVENT_ERROR:
  717. state->state = TFTP_STATE_FIN;
  718. setpacketevent(&state->spacket, TFTP_EVENT_ERROR);
  719. setpacketblock(&state->spacket, state->block);
  720. (void)sendto(state->sockfd, (void *)state->spacket.data, 4, SEND_4TH_ARG,
  721. (struct sockaddr *)&state->remote_addr,
  722. state->remote_addrlen);
  723. /* don't bother with the return code, but if the socket is still up we
  724. * should be a good TFTP client and let the server know we're done */
  725. state->state = TFTP_STATE_FIN;
  726. break;
  727. default:
  728. failf(data, "tftp_tx: internal error, event: %i", (int)(event));
  729. break;
  730. }
  731. return result;
  732. }
  733. /**********************************************************
  734. *
  735. * tftp_translate_code
  736. *
  737. * Translate internal error codes to CURL error codes
  738. *
  739. **********************************************************/
  740. static CURLcode tftp_translate_code(tftp_error_t error)
  741. {
  742. CURLcode result = CURLE_OK;
  743. if(error != TFTP_ERR_NONE) {
  744. switch(error) {
  745. case TFTP_ERR_NOTFOUND:
  746. result = CURLE_TFTP_NOTFOUND;
  747. break;
  748. case TFTP_ERR_PERM:
  749. result = CURLE_TFTP_PERM;
  750. break;
  751. case TFTP_ERR_DISKFULL:
  752. result = CURLE_REMOTE_DISK_FULL;
  753. break;
  754. case TFTP_ERR_UNDEF:
  755. case TFTP_ERR_ILLEGAL:
  756. result = CURLE_TFTP_ILLEGAL;
  757. break;
  758. case TFTP_ERR_UNKNOWNID:
  759. result = CURLE_TFTP_UNKNOWNID;
  760. break;
  761. case TFTP_ERR_EXISTS:
  762. result = CURLE_REMOTE_FILE_EXISTS;
  763. break;
  764. case TFTP_ERR_NOSUCHUSER:
  765. result = CURLE_TFTP_NOSUCHUSER;
  766. break;
  767. case TFTP_ERR_TIMEOUT:
  768. result = CURLE_OPERATION_TIMEDOUT;
  769. break;
  770. case TFTP_ERR_NORESPONSE:
  771. result = CURLE_COULDNT_CONNECT;
  772. break;
  773. default:
  774. result = CURLE_ABORTED_BY_CALLBACK;
  775. break;
  776. }
  777. }
  778. else
  779. result = CURLE_OK;
  780. return result;
  781. }
  782. /**********************************************************
  783. *
  784. * tftp_state_machine
  785. *
  786. * The tftp state machine event dispatcher
  787. *
  788. **********************************************************/
  789. static CURLcode tftp_state_machine(tftp_state_data_t *state,
  790. tftp_event_t event)
  791. {
  792. CURLcode result = CURLE_OK;
  793. struct Curl_easy *data = state->conn->data;
  794. switch(state->state) {
  795. case TFTP_STATE_START:
  796. DEBUGF(infof(data, "TFTP_STATE_START\n"));
  797. result = tftp_send_first(state, event);
  798. break;
  799. case TFTP_STATE_RX:
  800. DEBUGF(infof(data, "TFTP_STATE_RX\n"));
  801. result = tftp_rx(state, event);
  802. break;
  803. case TFTP_STATE_TX:
  804. DEBUGF(infof(data, "TFTP_STATE_TX\n"));
  805. result = tftp_tx(state, event);
  806. break;
  807. case TFTP_STATE_FIN:
  808. infof(data, "%s\n", "TFTP finished");
  809. break;
  810. default:
  811. DEBUGF(infof(data, "STATE: %d\n", state->state));
  812. failf(data, "%s", "Internal state machine error");
  813. result = CURLE_TFTP_ILLEGAL;
  814. break;
  815. }
  816. return result;
  817. }
  818. /**********************************************************
  819. *
  820. * tftp_disconnect
  821. *
  822. * The disconnect callback
  823. *
  824. **********************************************************/
  825. static CURLcode tftp_disconnect(struct connectdata *conn, bool dead_connection)
  826. {
  827. tftp_state_data_t *state = conn->proto.tftpc;
  828. (void) dead_connection;
  829. /* done, free dynamically allocated pkt buffers */
  830. if(state) {
  831. Curl_safefree(state->rpacket.data);
  832. Curl_safefree(state->spacket.data);
  833. free(state);
  834. }
  835. return CURLE_OK;
  836. }
  837. /**********************************************************
  838. *
  839. * tftp_connect
  840. *
  841. * The connect callback
  842. *
  843. **********************************************************/
  844. static CURLcode tftp_connect(struct connectdata *conn, bool *done)
  845. {
  846. tftp_state_data_t *state;
  847. int blksize, rc;
  848. blksize = TFTP_BLKSIZE_DEFAULT;
  849. state = conn->proto.tftpc = calloc(1, sizeof(tftp_state_data_t));
  850. if(!state)
  851. return CURLE_OUT_OF_MEMORY;
  852. /* alloc pkt buffers based on specified blksize */
  853. if(conn->data->set.tftp_blksize) {
  854. blksize = (int)conn->data->set.tftp_blksize;
  855. if(blksize > TFTP_BLKSIZE_MAX || blksize < TFTP_BLKSIZE_MIN)
  856. return CURLE_TFTP_ILLEGAL;
  857. }
  858. if(!state->rpacket.data) {
  859. state->rpacket.data = calloc(1, blksize + 2 + 2);
  860. if(!state->rpacket.data)
  861. return CURLE_OUT_OF_MEMORY;
  862. }
  863. if(!state->spacket.data) {
  864. state->spacket.data = calloc(1, blksize + 2 + 2);
  865. if(!state->spacket.data)
  866. return CURLE_OUT_OF_MEMORY;
  867. }
  868. /* we don't keep TFTP connections up basically because there's none or very
  869. * little gain for UDP */
  870. connclose(conn, "TFTP");
  871. state->conn = conn;
  872. state->sockfd = state->conn->sock[FIRSTSOCKET];
  873. state->state = TFTP_STATE_START;
  874. state->error = TFTP_ERR_NONE;
  875. state->blksize = TFTP_BLKSIZE_DEFAULT;
  876. state->requested_blksize = blksize;
  877. ((struct sockaddr *)&state->local_addr)->sa_family =
  878. (unsigned short)(conn->ip_addr->ai_family);
  879. tftp_set_timeouts(state);
  880. if(!conn->bits.bound) {
  881. /* If not already bound, bind to any interface, random UDP port. If it is
  882. * reused or a custom local port was desired, this has already been done!
  883. *
  884. * We once used the size of the local_addr struct as the third argument
  885. * for bind() to better work with IPv6 or whatever size the struct could
  886. * have, but we learned that at least Tru64, AIX and IRIX *requires* the
  887. * size of that argument to match the exact size of a 'sockaddr_in' struct
  888. * when running IPv4-only.
  889. *
  890. * Therefore we use the size from the address we connected to, which we
  891. * assume uses the same IP version and thus hopefully this works for both
  892. * IPv4 and IPv6...
  893. */
  894. rc = bind(state->sockfd, (struct sockaddr *)&state->local_addr,
  895. conn->ip_addr->ai_addrlen);
  896. if(rc) {
  897. failf(conn->data, "bind() failed; %s",
  898. Curl_strerror(conn, SOCKERRNO));
  899. return CURLE_COULDNT_CONNECT;
  900. }
  901. conn->bits.bound = TRUE;
  902. }
  903. Curl_pgrsStartNow(conn->data);
  904. *done = TRUE;
  905. return CURLE_OK;
  906. }
  907. /**********************************************************
  908. *
  909. * tftp_done
  910. *
  911. * The done callback
  912. *
  913. **********************************************************/
  914. static CURLcode tftp_done(struct connectdata *conn, CURLcode status,
  915. bool premature)
  916. {
  917. CURLcode result = CURLE_OK;
  918. tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc;
  919. (void)status; /* unused */
  920. (void)premature; /* not used */
  921. if(Curl_pgrsDone(conn))
  922. return CURLE_ABORTED_BY_CALLBACK;
  923. /* If we have encountered an error */
  924. if(state)
  925. result = tftp_translate_code(state->error);
  926. return result;
  927. }
  928. /**********************************************************
  929. *
  930. * tftp_getsock
  931. *
  932. * The getsock callback
  933. *
  934. **********************************************************/
  935. static int tftp_getsock(struct connectdata *conn, curl_socket_t *socks,
  936. int numsocks)
  937. {
  938. if(!numsocks)
  939. return GETSOCK_BLANK;
  940. socks[0] = conn->sock[FIRSTSOCKET];
  941. return GETSOCK_READSOCK(0);
  942. }
  943. /**********************************************************
  944. *
  945. * tftp_receive_packet
  946. *
  947. * Called once select fires and data is ready on the socket
  948. *
  949. **********************************************************/
  950. static CURLcode tftp_receive_packet(struct connectdata *conn)
  951. {
  952. struct Curl_sockaddr_storage fromaddr;
  953. curl_socklen_t fromlen;
  954. CURLcode result = CURLE_OK;
  955. struct Curl_easy *data = conn->data;
  956. tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc;
  957. struct SingleRequest *k = &data->req;
  958. /* Receive the packet */
  959. fromlen = sizeof(fromaddr);
  960. state->rbytes = (int)recvfrom(state->sockfd,
  961. (void *)state->rpacket.data,
  962. state->blksize+4,
  963. 0,
  964. (struct sockaddr *)&fromaddr,
  965. &fromlen);
  966. if(state->remote_addrlen==0) {
  967. memcpy(&state->remote_addr, &fromaddr, fromlen);
  968. state->remote_addrlen = fromlen;
  969. }
  970. /* Sanity check packet length */
  971. if(state->rbytes < 4) {
  972. failf(data, "Received too short packet");
  973. /* Not a timeout, but how best to handle it? */
  974. state->event = TFTP_EVENT_TIMEOUT;
  975. }
  976. else {
  977. /* The event is given by the TFTP packet time */
  978. state->event = (tftp_event_t)getrpacketevent(&state->rpacket);
  979. switch(state->event) {
  980. case TFTP_EVENT_DATA:
  981. /* Don't pass to the client empty or retransmitted packets */
  982. if(state->rbytes > 4 &&
  983. (NEXT_BLOCKNUM(state->block) == getrpacketblock(&state->rpacket))) {
  984. result = Curl_client_write(conn, CLIENTWRITE_BODY,
  985. (char *)state->rpacket.data+4,
  986. state->rbytes-4);
  987. if(result) {
  988. tftp_state_machine(state, TFTP_EVENT_ERROR);
  989. return result;
  990. }
  991. k->bytecount += state->rbytes-4;
  992. Curl_pgrsSetDownloadCounter(data, (curl_off_t) k->bytecount);
  993. }
  994. break;
  995. case TFTP_EVENT_ERROR:
  996. state->error = (tftp_error_t)getrpacketblock(&state->rpacket);
  997. infof(data, "%s\n", (const char *)state->rpacket.data+4);
  998. break;
  999. case TFTP_EVENT_ACK:
  1000. break;
  1001. case TFTP_EVENT_OACK:
  1002. result = tftp_parse_option_ack(state,
  1003. (const char *)state->rpacket.data+2,
  1004. state->rbytes-2);
  1005. if(result)
  1006. return result;
  1007. break;
  1008. case TFTP_EVENT_RRQ:
  1009. case TFTP_EVENT_WRQ:
  1010. default:
  1011. failf(data, "%s", "Internal error: Unexpected packet");
  1012. break;
  1013. }
  1014. /* Update the progress meter */
  1015. if(Curl_pgrsUpdate(conn)) {
  1016. tftp_state_machine(state, TFTP_EVENT_ERROR);
  1017. return CURLE_ABORTED_BY_CALLBACK;
  1018. }
  1019. }
  1020. return result;
  1021. }
  1022. /**********************************************************
  1023. *
  1024. * tftp_state_timeout
  1025. *
  1026. * Check if timeouts have been reached
  1027. *
  1028. **********************************************************/
  1029. static long tftp_state_timeout(struct connectdata *conn, tftp_event_t *event)
  1030. {
  1031. time_t current;
  1032. tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc;
  1033. if(event)
  1034. *event = TFTP_EVENT_NONE;
  1035. time(&current);
  1036. if(current > state->max_time) {
  1037. DEBUGF(infof(conn->data, "timeout: %ld > %ld\n",
  1038. (long)current, (long)state->max_time));
  1039. state->error = TFTP_ERR_TIMEOUT;
  1040. state->state = TFTP_STATE_FIN;
  1041. return 0;
  1042. }
  1043. else if(current > state->rx_time+state->retry_time) {
  1044. if(event)
  1045. *event = TFTP_EVENT_TIMEOUT;
  1046. time(&state->rx_time); /* update even though we received nothing */
  1047. }
  1048. /* there's a typecast below here since 'time_t' may in fact be larger than
  1049. 'long', but we estimate that a 'long' will still be able to hold number
  1050. of seconds even if "only" 32 bit */
  1051. return (long)(state->max_time - current);
  1052. }
  1053. /**********************************************************
  1054. *
  1055. * tftp_multi_statemach
  1056. *
  1057. * Handle single RX socket event and return
  1058. *
  1059. **********************************************************/
  1060. static CURLcode tftp_multi_statemach(struct connectdata *conn, bool *done)
  1061. {
  1062. int rc;
  1063. tftp_event_t event;
  1064. CURLcode result = CURLE_OK;
  1065. struct Curl_easy *data = conn->data;
  1066. tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc;
  1067. long timeout_ms = tftp_state_timeout(conn, &event);
  1068. *done = FALSE;
  1069. if(timeout_ms <= 0) {
  1070. failf(data, "TFTP response timeout");
  1071. return CURLE_OPERATION_TIMEDOUT;
  1072. }
  1073. else if(event != TFTP_EVENT_NONE) {
  1074. result = tftp_state_machine(state, event);
  1075. if(result)
  1076. return result;
  1077. *done = (state->state == TFTP_STATE_FIN) ? TRUE : FALSE;
  1078. if(*done)
  1079. /* Tell curl we're done */
  1080. Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
  1081. }
  1082. else {
  1083. /* no timeouts to handle, check our socket */
  1084. rc = SOCKET_READABLE(state->sockfd, 0);
  1085. if(rc == -1) {
  1086. /* bail out */
  1087. int error = SOCKERRNO;
  1088. failf(data, "%s", Curl_strerror(conn, error));
  1089. state->event = TFTP_EVENT_ERROR;
  1090. }
  1091. else if(rc != 0) {
  1092. result = tftp_receive_packet(conn);
  1093. if(result)
  1094. return result;
  1095. result = tftp_state_machine(state, state->event);
  1096. if(result)
  1097. return result;
  1098. *done = (state->state == TFTP_STATE_FIN) ? TRUE : FALSE;
  1099. if(*done)
  1100. /* Tell curl we're done */
  1101. Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
  1102. }
  1103. /* if rc == 0, then select() timed out */
  1104. }
  1105. return result;
  1106. }
  1107. /**********************************************************
  1108. *
  1109. * tftp_doing
  1110. *
  1111. * Called from multi.c while DOing
  1112. *
  1113. **********************************************************/
  1114. static CURLcode tftp_doing(struct connectdata *conn, bool *dophase_done)
  1115. {
  1116. CURLcode result;
  1117. result = tftp_multi_statemach(conn, dophase_done);
  1118. if(*dophase_done) {
  1119. DEBUGF(infof(conn->data, "DO phase is complete\n"));
  1120. }
  1121. else if(!result) {
  1122. /* The multi code doesn't have this logic for the DOING state so we
  1123. provide it for TFTP since it may do the entire transfer in this
  1124. state. */
  1125. if(Curl_pgrsUpdate(conn))
  1126. result = CURLE_ABORTED_BY_CALLBACK;
  1127. else
  1128. result = Curl_speedcheck(conn->data, Curl_tvnow());
  1129. }
  1130. return result;
  1131. }
  1132. /**********************************************************
  1133. *
  1134. * tftp_peform
  1135. *
  1136. * Entry point for transfer from tftp_do, sarts state mach
  1137. *
  1138. **********************************************************/
  1139. static CURLcode tftp_perform(struct connectdata *conn, bool *dophase_done)
  1140. {
  1141. CURLcode result = CURLE_OK;
  1142. tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc;
  1143. *dophase_done = FALSE;
  1144. result = tftp_state_machine(state, TFTP_EVENT_INIT);
  1145. if((state->state == TFTP_STATE_FIN) || result)
  1146. return result;
  1147. tftp_multi_statemach(conn, dophase_done);
  1148. if(*dophase_done)
  1149. DEBUGF(infof(conn->data, "DO phase is complete\n"));
  1150. return result;
  1151. }
  1152. /**********************************************************
  1153. *
  1154. * tftp_do
  1155. *
  1156. * The do callback
  1157. *
  1158. * This callback initiates the TFTP transfer
  1159. *
  1160. **********************************************************/
  1161. static CURLcode tftp_do(struct connectdata *conn, bool *done)
  1162. {
  1163. tftp_state_data_t *state;
  1164. CURLcode result;
  1165. *done = FALSE;
  1166. if(!conn->proto.tftpc) {
  1167. result = tftp_connect(conn, done);
  1168. if(result)
  1169. return result;
  1170. }
  1171. state = (tftp_state_data_t *)conn->proto.tftpc;
  1172. if(!state)
  1173. return CURLE_BAD_CALLING_ORDER;
  1174. result = tftp_perform(conn, done);
  1175. /* If tftp_perform() returned an error, use that for return code. If it
  1176. was OK, see if tftp_translate_code() has an error. */
  1177. if(!result)
  1178. /* If we have encountered an internal tftp error, translate it. */
  1179. result = tftp_translate_code(state->error);
  1180. return result;
  1181. }
  1182. static CURLcode tftp_setup_connection(struct connectdata * conn)
  1183. {
  1184. struct Curl_easy *data = conn->data;
  1185. char *type;
  1186. char command;
  1187. conn->socktype = SOCK_DGRAM; /* UDP datagram based */
  1188. /* TFTP URLs support an extension like ";mode=<typecode>" that
  1189. * we'll try to get now! */
  1190. type = strstr(data->state.path, ";mode=");
  1191. if(!type)
  1192. type = strstr(conn->host.rawalloc, ";mode=");
  1193. if(type) {
  1194. *type = 0; /* it was in the middle of the hostname */
  1195. command = Curl_raw_toupper(type[6]);
  1196. switch(command) {
  1197. case 'A': /* ASCII mode */
  1198. case 'N': /* NETASCII mode */
  1199. data->set.prefer_ascii = TRUE;
  1200. break;
  1201. case 'O': /* octet mode */
  1202. case 'I': /* binary mode */
  1203. default:
  1204. /* switch off ASCII */
  1205. data->set.prefer_ascii = FALSE;
  1206. break;
  1207. }
  1208. }
  1209. return CURLE_OK;
  1210. }
  1211. #endif