tftp.c 42 KB

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