tftp.c 42 KB

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