tftp.c 41 KB

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