tftp.c 40 KB

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