tftp.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2007, 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 http://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. * $Id$
  22. ***************************************************************************/
  23. #include "setup.h"
  24. #ifndef CURL_DISABLE_TFTP
  25. /* -- WIN32 approved -- */
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <stdarg.h>
  29. #include <stdlib.h>
  30. #include <ctype.h>
  31. #ifdef HAVE_SYS_TYPES_H
  32. #include <sys/types.h>
  33. #endif
  34. #ifdef HAVE_SYS_STAT_H
  35. #include <sys/stat.h>
  36. #endif
  37. #if defined(WIN32)
  38. #include <time.h>
  39. #include <io.h>
  40. #else
  41. #ifdef HAVE_SYS_SOCKET_H
  42. #include <sys/socket.h>
  43. #endif
  44. #include <netinet/in.h>
  45. #ifdef HAVE_SYS_TIME_H
  46. #include <sys/time.h>
  47. #endif
  48. #ifdef HAVE_UNISTD_H
  49. #include <unistd.h>
  50. #endif
  51. #include <netdb.h>
  52. #ifdef HAVE_ARPA_INET_H
  53. #include <arpa/inet.h>
  54. #endif
  55. #ifdef HAVE_NET_IF_H
  56. #include <net/if.h>
  57. #endif
  58. #include <sys/ioctl.h>
  59. #include <signal.h>
  60. #ifdef HAVE_SYS_PARAM_H
  61. #include <sys/param.h>
  62. #endif
  63. #endif
  64. #include "urldata.h"
  65. #include <curl/curl.h>
  66. #include "transfer.h"
  67. #include "sendf.h"
  68. #include "tftp.h"
  69. #include "progress.h"
  70. #include "connect.h"
  71. #include "strerror.h"
  72. #include "sockaddr.h" /* required for Curl_sockaddr_storage */
  73. #include "url.h"
  74. #define _MPRINTF_REPLACE /* use our functions only */
  75. #include <curl/mprintf.h>
  76. #include "memory.h"
  77. #include "select.h"
  78. /* The last #include file should be: */
  79. #include "memdebug.h"
  80. /* RFC2348 allows the block size to be negotiated, but we don't support that */
  81. #define TFTP_BLOCKSIZE 512
  82. typedef enum {
  83. TFTP_MODE_NETASCII=0,
  84. TFTP_MODE_OCTET
  85. } tftp_mode_t;
  86. typedef enum {
  87. TFTP_STATE_START=0,
  88. TFTP_STATE_RX,
  89. TFTP_STATE_TX,
  90. TFTP_STATE_FIN
  91. } tftp_state_t;
  92. typedef enum {
  93. TFTP_EVENT_INIT=0,
  94. TFTP_EVENT_RRQ = 1,
  95. TFTP_EVENT_WRQ = 2,
  96. TFTP_EVENT_DATA = 3,
  97. TFTP_EVENT_ACK = 4,
  98. TFTP_EVENT_ERROR = 5,
  99. TFTP_EVENT_TIMEOUT
  100. } tftp_event_t;
  101. typedef enum {
  102. TFTP_ERR_UNDEF=0,
  103. TFTP_ERR_NOTFOUND,
  104. TFTP_ERR_PERM,
  105. TFTP_ERR_DISKFULL,
  106. TFTP_ERR_ILLEGAL,
  107. TFTP_ERR_UNKNOWNID,
  108. TFTP_ERR_EXISTS,
  109. TFTP_ERR_NOSUCHUSER,
  110. TFTP_ERR_TIMEOUT,
  111. TFTP_ERR_NORESPONSE
  112. } tftp_error_t;
  113. typedef struct tftp_packet {
  114. unsigned char data[2 + 2 + TFTP_BLOCKSIZE];
  115. } tftp_packet_t;
  116. typedef struct tftp_state_data {
  117. tftp_state_t state;
  118. tftp_mode_t mode;
  119. tftp_error_t error;
  120. struct connectdata *conn;
  121. curl_socket_t sockfd;
  122. int retries;
  123. int retry_time;
  124. int retry_max;
  125. time_t start_time;
  126. time_t max_time;
  127. unsigned short block;
  128. struct Curl_sockaddr_storage local_addr;
  129. socklen_t local_addrlen;
  130. struct Curl_sockaddr_storage remote_addr;
  131. socklen_t remote_addrlen;
  132. int rbytes;
  133. int sbytes;
  134. tftp_packet_t rpacket;
  135. tftp_packet_t spacket;
  136. } tftp_state_data_t;
  137. /* Forward declarations */
  138. static CURLcode tftp_rx(tftp_state_data_t *state, tftp_event_t event) ;
  139. static CURLcode tftp_tx(tftp_state_data_t *state, tftp_event_t event) ;
  140. void tftp_set_timeouts(tftp_state_data_t *state) ;
  141. /**********************************************************
  142. *
  143. * tftp_set_timeouts -
  144. *
  145. * Set timeouts based on state machine state.
  146. * Use user provided connect timeouts until DATA or ACK
  147. * packet is received, then use user-provided transfer timeouts
  148. *
  149. *
  150. **********************************************************/
  151. void tftp_set_timeouts(tftp_state_data_t *state)
  152. {
  153. struct SessionHandle *data = state->conn->data;
  154. time_t maxtime, timeout;
  155. time(&state->start_time);
  156. if(state->state == TFTP_STATE_START) {
  157. /* Compute drop-dead time */
  158. maxtime = (time_t)(data->set.connecttimeout?data->set.connecttimeout:30);
  159. state->max_time = state->start_time+maxtime;
  160. /* Set per-block timeout to total */
  161. timeout = maxtime ;
  162. /* Average restart after 5 seconds */
  163. state->retry_max = (int)(timeout/5);
  164. /* Compute the re-start interval to suit the timeout */
  165. state->retry_time = (int)(timeout/state->retry_max);
  166. if(state->retry_time<1)
  167. state->retry_time=1;
  168. }
  169. else {
  170. /* Compute drop-dead time */
  171. maxtime = (time_t)(data->set.timeout?data->set.timeout:3600);
  172. state->max_time = state->start_time+maxtime;
  173. /* Set per-block timeout to 10% of total */
  174. timeout = maxtime/10 ;
  175. /* Average reposting an ACK after 15 seconds */
  176. state->retry_max = (int)(timeout/15);
  177. }
  178. /* But bound the total number */
  179. if(state->retry_max<3)
  180. state->retry_max=3;
  181. if(state->retry_max>50)
  182. state->retry_max=50;
  183. /* Compute the re-ACK interval to suit the timeout */
  184. state->retry_time = (int)(timeout/state->retry_max);
  185. if(state->retry_time<1)
  186. state->retry_time=1;
  187. infof(data, "set timeouts for state %d; Total %d, retry %d maxtry %d\n",
  188. state->state, (state->max_time-state->start_time),
  189. state->retry_time, state->retry_max);
  190. }
  191. /**********************************************************
  192. *
  193. * tftp_set_send_first
  194. *
  195. * Event handler for the START state
  196. *
  197. **********************************************************/
  198. static void setpacketevent(tftp_packet_t *packet, unsigned short num)
  199. {
  200. packet->data[0] = (unsigned char)(num >> 8);
  201. packet->data[1] = (unsigned char)(num & 0xff);
  202. }
  203. static void setpacketblock(tftp_packet_t *packet, unsigned short num)
  204. {
  205. packet->data[2] = (unsigned char)(num >> 8);
  206. packet->data[3] = (unsigned char)(num & 0xff);
  207. }
  208. static unsigned short getrpacketevent(tftp_packet_t *packet)
  209. {
  210. return (unsigned short)((packet->data[0] << 8) | packet->data[1]);
  211. }
  212. static unsigned short getrpacketblock(tftp_packet_t *packet)
  213. {
  214. return (unsigned short)((packet->data[2] << 8) | packet->data[3]);
  215. }
  216. static CURLcode tftp_send_first(tftp_state_data_t *state, tftp_event_t event)
  217. {
  218. int sbytes;
  219. const char *mode = "octet";
  220. char *filename;
  221. struct SessionHandle *data = state->conn->data;
  222. CURLcode res = CURLE_OK;
  223. /* Set ascii mode if -B flag was used */
  224. if(data->set.prefer_ascii)
  225. mode = "netascii";
  226. switch(event) {
  227. case TFTP_EVENT_INIT: /* Send the first packet out */
  228. case TFTP_EVENT_TIMEOUT: /* Resend the first packet out */
  229. /* Increment the retry counter, quit if over the limit */
  230. state->retries++;
  231. if(state->retries>state->retry_max) {
  232. state->error = TFTP_ERR_NORESPONSE;
  233. state->state = TFTP_STATE_FIN;
  234. return res;
  235. }
  236. if(data->set.upload) {
  237. /* If we are uploading, send an WRQ */
  238. setpacketevent(&state->spacket, TFTP_EVENT_WRQ);
  239. state->conn->data->reqdata.upload_fromhere = (char *)&state->spacket.data[4];
  240. if(data->set.infilesize != -1)
  241. Curl_pgrsSetUploadSize(data, data->set.infilesize);
  242. }
  243. else {
  244. /* If we are downloading, send an RRQ */
  245. setpacketevent(&state->spacket, TFTP_EVENT_RRQ);
  246. }
  247. /* As RFC3617 describes the separator slash is not actually part of the
  248. file name so we skip the always-present first letter of the path string. */
  249. filename = curl_easy_unescape(data, &state->conn->data->reqdata.path[1], 0,
  250. NULL);
  251. snprintf((char *)&state->spacket.data[2],
  252. TFTP_BLOCKSIZE,
  253. "%s%c%s%c", filename, '\0', mode, '\0');
  254. sbytes = 4 + (int)strlen(filename) + (int)strlen(mode);
  255. sbytes = sendto(state->sockfd, (void *)&state->spacket,
  256. sbytes, 0,
  257. state->conn->ip_addr->ai_addr,
  258. state->conn->ip_addr->ai_addrlen);
  259. if(sbytes < 0) {
  260. failf(data, "%s\n", Curl_strerror(state->conn, Curl_sockerrno()));
  261. }
  262. Curl_safefree(filename);
  263. break;
  264. case TFTP_EVENT_ACK: /* Connected for transmit */
  265. infof(data, "%s\n", "Connected for transmit");
  266. state->state = TFTP_STATE_TX;
  267. tftp_set_timeouts(state);
  268. return tftp_tx(state, event);
  269. case TFTP_EVENT_DATA: /* connected for receive */
  270. infof(data, "%s\n", "Connected for receive");
  271. state->state = TFTP_STATE_RX;
  272. tftp_set_timeouts(state);
  273. return tftp_rx(state, event);
  274. case TFTP_EVENT_ERROR:
  275. state->state = TFTP_STATE_FIN;
  276. break;
  277. default:
  278. failf(state->conn->data, "tftp_send_first: internal error\n");
  279. break;
  280. }
  281. return res;
  282. }
  283. /**********************************************************
  284. *
  285. * tftp_rx
  286. *
  287. * Event handler for the RX state
  288. *
  289. **********************************************************/
  290. static CURLcode tftp_rx(tftp_state_data_t *state, tftp_event_t event)
  291. {
  292. int sbytes;
  293. int rblock;
  294. struct SessionHandle *data = state->conn->data;
  295. switch(event) {
  296. case TFTP_EVENT_DATA:
  297. /* Is this the block we expect? */
  298. rblock = getrpacketblock(&state->rpacket);
  299. if ((state->block+1) != rblock) {
  300. /* No, log it, up the retry count and fail if over the limit */
  301. infof(data,
  302. "Received unexpected DATA packet block %d\n", rblock);
  303. state->retries++;
  304. if (state->retries>state->retry_max) {
  305. failf(data, "tftp_rx: giving up waiting for block %d\n",
  306. state->block+1);
  307. return CURLE_TFTP_ILLEGAL;
  308. }
  309. }
  310. /* This is the expected block. Reset counters and ACK it. */
  311. state->block = (unsigned short)rblock;
  312. state->retries = 0;
  313. setpacketevent(&state->spacket, TFTP_EVENT_ACK);
  314. setpacketblock(&state->spacket, state->block);
  315. sbytes = sendto(state->sockfd, (void *)state->spacket.data,
  316. 4, SEND_4TH_ARG,
  317. (struct sockaddr *)&state->remote_addr,
  318. state->remote_addrlen);
  319. if(sbytes < 0) {
  320. failf(data, "%s\n", Curl_strerror(state->conn, Curl_sockerrno()));
  321. }
  322. /* Check if completed (That is, a less than full packet is received) */
  323. if (state->rbytes < (int)sizeof(state->spacket)){
  324. state->state = TFTP_STATE_FIN;
  325. }
  326. else {
  327. state->state = TFTP_STATE_RX;
  328. }
  329. break;
  330. case TFTP_EVENT_TIMEOUT:
  331. /* Increment the retry count and fail if over the limit */
  332. state->retries++;
  333. infof(data,
  334. "Timeout waiting for block %d ACK. Retries = %d\n", state->retries);
  335. if(state->retries > state->retry_max) {
  336. state->error = TFTP_ERR_TIMEOUT;
  337. state->state = TFTP_STATE_FIN;
  338. }
  339. else {
  340. /* Resend the previous ACK */
  341. sbytes = sendto(state->sockfd, (void *)&state->spacket,
  342. 4, SEND_4TH_ARG,
  343. (struct sockaddr *)&state->remote_addr,
  344. state->remote_addrlen);
  345. /* Check all sbytes were sent */
  346. if(sbytes<0) {
  347. failf(data, "%s\n", Curl_strerror(state->conn, Curl_sockerrno()));
  348. }
  349. }
  350. break;
  351. case TFTP_EVENT_ERROR:
  352. state->state = TFTP_STATE_FIN;
  353. break;
  354. default:
  355. failf(data, "%s\n", "tftp_rx: internal error");
  356. break;
  357. }
  358. Curl_pgrsSetDownloadCounter(data,
  359. (curl_off_t) state->block*TFTP_BLOCKSIZE);
  360. return CURLE_OK;
  361. }
  362. /**********************************************************
  363. *
  364. * tftp_tx
  365. *
  366. * Event handler for the TX state
  367. *
  368. **********************************************************/
  369. static CURLcode tftp_tx(tftp_state_data_t *state, tftp_event_t event)
  370. {
  371. struct SessionHandle *data = state->conn->data;
  372. int sbytes;
  373. int rblock;
  374. CURLcode res = CURLE_OK;
  375. switch(event) {
  376. case TFTP_EVENT_ACK:
  377. /* Ack the packet */
  378. rblock = getrpacketblock(&state->rpacket);
  379. if(rblock != state->block) {
  380. /* This isn't the expected block. Log it and up the retry counter */
  381. infof(data, "Received ACK for block %d, expecting %d\n",
  382. rblock, state->block);
  383. state->retries++;
  384. /* Bail out if over the maximum */
  385. if(state->retries>state->retry_max) {
  386. failf(data, "tftp_tx: giving up waiting for block %d ack",
  387. state->block);
  388. res = CURLE_SEND_ERROR;
  389. }
  390. else {
  391. /* Re-send the data packet */
  392. sbytes = sendto(state->sockfd, (void *)&state->spacket,
  393. 4+state->sbytes, SEND_4TH_ARG,
  394. (struct sockaddr *)&state->remote_addr,
  395. state->remote_addrlen);
  396. /* Check all sbytes were sent */
  397. if(sbytes<0) {
  398. failf(data, "%s\n", Curl_strerror(state->conn, Curl_sockerrno()));
  399. res = CURLE_SEND_ERROR;
  400. }
  401. }
  402. return res;
  403. }
  404. /* This is the expected packet. Reset the counters and send the next
  405. block */
  406. state->block++;
  407. state->retries = 0;
  408. setpacketevent(&state->spacket, TFTP_EVENT_DATA);
  409. setpacketblock(&state->spacket, state->block);
  410. if(state->block > 1 && state->sbytes < TFTP_BLOCKSIZE) {
  411. state->state = TFTP_STATE_FIN;
  412. return CURLE_OK;
  413. }
  414. res = Curl_fillreadbuffer(state->conn, TFTP_BLOCKSIZE, &state->sbytes);
  415. if(res)
  416. return res;
  417. sbytes = sendto(state->sockfd, (void *)state->spacket.data,
  418. 4+state->sbytes, SEND_4TH_ARG,
  419. (struct sockaddr *)&state->remote_addr,
  420. state->remote_addrlen);
  421. /* Check all sbytes were sent */
  422. if(sbytes<0) {
  423. failf(data, "%s\n", Curl_strerror(state->conn, Curl_sockerrno()));
  424. }
  425. break;
  426. case TFTP_EVENT_TIMEOUT:
  427. /* Increment the retry counter and log the timeout */
  428. state->retries++;
  429. infof(data, "Timeout waiting for block %d ACK. "
  430. " Retries = %d\n", state->retries);
  431. /* Decide if we've had enough */
  432. if(state->retries > state->retry_max) {
  433. state->error = TFTP_ERR_TIMEOUT;
  434. state->state = TFTP_STATE_FIN;
  435. } else {
  436. /* Re-send the data packet */
  437. sbytes = sendto(state->sockfd, (void *)&state->spacket,
  438. 4+state->sbytes, SEND_4TH_ARG,
  439. (struct sockaddr *)&state->remote_addr,
  440. state->remote_addrlen);
  441. /* Check all sbytes were sent */
  442. if(sbytes<0) {
  443. failf(data, "%s\n", Curl_strerror(state->conn, Curl_sockerrno()));
  444. }
  445. }
  446. break;
  447. case TFTP_EVENT_ERROR:
  448. state->state = TFTP_STATE_FIN;
  449. break;
  450. default:
  451. failf(data, "%s\n", "tftp_tx: internal error");
  452. break;
  453. }
  454. /* Update the progress meter */
  455. Curl_pgrsSetUploadCounter(data, (curl_off_t) state->block*TFTP_BLOCKSIZE);
  456. return res;
  457. }
  458. /**********************************************************
  459. *
  460. * tftp_state_machine
  461. *
  462. * The tftp state machine event dispatcher
  463. *
  464. **********************************************************/
  465. static CURLcode tftp_state_machine(tftp_state_data_t *state,
  466. tftp_event_t event)
  467. {
  468. CURLcode res = CURLE_OK;
  469. struct SessionHandle *data = state->conn->data;
  470. switch(state->state) {
  471. case TFTP_STATE_START:
  472. DEBUGF(infof(data, "TFTP_STATE_START\n"));
  473. res = tftp_send_first(state, event);
  474. break;
  475. case TFTP_STATE_RX:
  476. DEBUGF(infof(data, "TFTP_STATE_RX\n"));
  477. res = tftp_rx(state, event);
  478. break;
  479. case TFTP_STATE_TX:
  480. DEBUGF(infof(data, "TFTP_STATE_TX\n"));
  481. res = tftp_tx(state, event);
  482. break;
  483. case TFTP_STATE_FIN:
  484. infof(data, "%s\n", "TFTP finished");
  485. break;
  486. default:
  487. DEBUGF(infof(data, "STATE: %d\n", state->state));
  488. failf(data, "%s\n", "Internal state machine error");
  489. res = CURLE_TFTP_ILLEGAL;
  490. break;
  491. }
  492. return res;
  493. }
  494. /**********************************************************
  495. *
  496. * Curl_tftp_connect
  497. *
  498. * The connect callback
  499. *
  500. **********************************************************/
  501. CURLcode Curl_tftp_connect(struct connectdata *conn, bool *done)
  502. {
  503. CURLcode code;
  504. tftp_state_data_t *state;
  505. int rc;
  506. state = conn->data->reqdata.proto.tftp = calloc(sizeof(tftp_state_data_t),
  507. 1);
  508. if(!state)
  509. return CURLE_OUT_OF_MEMORY;
  510. conn->bits.close = FALSE; /* keep it open if possible */
  511. state->conn = conn;
  512. state->sockfd = state->conn->sock[FIRSTSOCKET];
  513. state->state = TFTP_STATE_START;
  514. ((struct sockaddr *)&state->local_addr)->sa_family =
  515. conn->ip_addr->ai_family;
  516. tftp_set_timeouts(state);
  517. if(!conn->bits.reuse) {
  518. /* If not reused, bind to any interface, random UDP port. If it is reused,
  519. * this has already been done!
  520. *
  521. * We once used the size of the local_addr struct as the third argument for
  522. * bind() to better work with IPv6 or whatever size the struct could have,
  523. * but we learned that at least Tru64, AIX and IRIX *requires* the size of
  524. * that argument to match the exact size of a 'sockaddr_in' struct when
  525. * running IPv4-only.
  526. *
  527. * Therefore we use the size from the address we connected to, which we
  528. * assume uses the same IP version and thus hopefully this works for both
  529. * IPv4 and IPv6...
  530. */
  531. rc = bind(state->sockfd, (struct sockaddr *)&state->local_addr,
  532. conn->ip_addr->ai_addrlen);
  533. if(rc) {
  534. failf(conn->data, "bind() failed; %s\n",
  535. Curl_strerror(conn, Curl_sockerrno()));
  536. return CURLE_COULDNT_CONNECT;
  537. }
  538. }
  539. Curl_pgrsStartNow(conn->data);
  540. *done = TRUE;
  541. code = CURLE_OK;
  542. return(code);
  543. }
  544. /**********************************************************
  545. *
  546. * Curl_tftp_done
  547. *
  548. * The done callback
  549. *
  550. **********************************************************/
  551. CURLcode Curl_tftp_done(struct connectdata *conn, CURLcode status,
  552. bool premature)
  553. {
  554. (void)status; /* unused */
  555. (void)premature; /* not used */
  556. #if 0
  557. free(conn->data->reqdata.proto.tftp);
  558. conn->data->reqdata.proto.tftp = NULL;
  559. #endif
  560. Curl_pgrsDone(conn);
  561. return CURLE_OK;
  562. }
  563. /**********************************************************
  564. *
  565. * Curl_tftp
  566. *
  567. * The do callback
  568. *
  569. * This callback handles the entire TFTP transfer
  570. *
  571. **********************************************************/
  572. CURLcode Curl_tftp(struct connectdata *conn, bool *done)
  573. {
  574. struct SessionHandle *data = conn->data;
  575. tftp_state_data_t *state =
  576. (tftp_state_data_t *) conn->data->reqdata.proto.tftp;
  577. tftp_event_t event;
  578. CURLcode code;
  579. int rc;
  580. struct Curl_sockaddr_storage fromaddr;
  581. socklen_t fromlen;
  582. int check_time = 0;
  583. *done = TRUE;
  584. /*
  585. Since connections can be re-used between SessionHandles, this might be a
  586. connection already existing but on a fresh SessionHandle struct so we must
  587. make sure we have a good 'struct TFTP' to play with. For new connections,
  588. the struct TFTP is allocated and setup in the Curl_tftp_connect() function.
  589. */
  590. if(!state) {
  591. code = Curl_tftp_connect(conn, done);
  592. if(code)
  593. return code;
  594. state = (tftp_state_data_t *)conn->data->reqdata.proto.tftp;
  595. }
  596. /* Run the TFTP State Machine */
  597. for(tftp_state_machine(state, TFTP_EVENT_INIT);
  598. state->state != TFTP_STATE_FIN;
  599. tftp_state_machine(state, event) ) {
  600. /* Wait until ready to read or timeout occurs */
  601. rc=Curl_select(state->sockfd, CURL_SOCKET_BAD, state->retry_time * 1000);
  602. if(rc == -1) {
  603. /* bail out */
  604. int error = Curl_sockerrno();
  605. failf(data, "%s\n", Curl_strerror(conn, error));
  606. event = TFTP_EVENT_ERROR;
  607. }
  608. else if (rc==0) {
  609. /* A timeout occured */
  610. event = TFTP_EVENT_TIMEOUT;
  611. /* Force a look at transfer timeouts */
  612. check_time = 0;
  613. }
  614. else {
  615. /* Receive the packet */
  616. fromlen=sizeof(fromaddr);
  617. state->rbytes = recvfrom(state->sockfd,
  618. (void *)&state->rpacket, sizeof(state->rpacket),
  619. 0, (struct sockaddr *)&fromaddr, &fromlen);
  620. if(state->remote_addrlen==0) {
  621. memcpy(&state->remote_addr, &fromaddr, fromlen);
  622. state->remote_addrlen = fromlen;
  623. }
  624. /* Sanity check packet length */
  625. if (state->rbytes < 4) {
  626. failf(conn->data, "Received too short packet\n");
  627. /* Not a timeout, but how best to handle it? */
  628. event = TFTP_EVENT_TIMEOUT;
  629. }
  630. else {
  631. /* The event is given by the TFTP packet time */
  632. event = (tftp_event_t)getrpacketevent(&state->rpacket);
  633. switch(event) {
  634. case TFTP_EVENT_DATA:
  635. /* Don't pass to the client empty or retransmitted packets */
  636. if (state->rbytes > 4 &&
  637. ((state->block+1) == getrpacketblock(&state->rpacket))) {
  638. code = Curl_client_write(conn, CLIENTWRITE_BODY,
  639. (char *)&state->rpacket.data[4],
  640. state->rbytes-4);
  641. if(code)
  642. return code;
  643. }
  644. break;
  645. case TFTP_EVENT_ERROR:
  646. state->error = (tftp_error_t)getrpacketblock(&state->rpacket);
  647. infof(conn->data, "%s\n", (char *)&state->rpacket.data[4]);
  648. break;
  649. case TFTP_EVENT_ACK:
  650. break;
  651. case TFTP_EVENT_RRQ:
  652. case TFTP_EVENT_WRQ:
  653. default:
  654. failf(conn->data, "%s\n", "Internal error: Unexpected packet");
  655. break;
  656. }
  657. /* Update the progress meter */
  658. if(Curl_pgrsUpdate(conn))
  659. return CURLE_ABORTED_BY_CALLBACK;
  660. }
  661. }
  662. /* Check for transfer timeout every 10 blocks, or after timeout */
  663. if(check_time%10==0) {
  664. time_t current;
  665. time(&current);
  666. if(current>state->max_time) {
  667. DEBUGF(infof(data, "timeout: %d > %d\n",
  668. current, state->max_time));
  669. state->error = TFTP_ERR_TIMEOUT;
  670. state->state = TFTP_STATE_FIN;
  671. }
  672. }
  673. }
  674. /* Tell curl we're done */
  675. code = Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
  676. if(code)
  677. return code;
  678. /* If we have encountered an error */
  679. if(state->error) {
  680. /* Translate internal error codes to curl error codes */
  681. switch(state->error) {
  682. case TFTP_ERR_NOTFOUND:
  683. code = CURLE_TFTP_NOTFOUND;
  684. break;
  685. case TFTP_ERR_PERM:
  686. code = CURLE_TFTP_PERM;
  687. break;
  688. case TFTP_ERR_DISKFULL:
  689. code = CURLE_TFTP_DISKFULL;
  690. break;
  691. case TFTP_ERR_ILLEGAL:
  692. code = CURLE_TFTP_ILLEGAL;
  693. break;
  694. case TFTP_ERR_UNKNOWNID:
  695. code = CURLE_TFTP_UNKNOWNID;
  696. break;
  697. case TFTP_ERR_EXISTS:
  698. code = CURLE_TFTP_EXISTS;
  699. break;
  700. case TFTP_ERR_NOSUCHUSER:
  701. code = CURLE_TFTP_NOSUCHUSER;
  702. break;
  703. case TFTP_ERR_TIMEOUT:
  704. code = CURLE_OPERATION_TIMEOUTED;
  705. break;
  706. case TFTP_ERR_NORESPONSE:
  707. code = CURLE_COULDNT_CONNECT;
  708. break;
  709. default:
  710. code= CURLE_ABORTED_BY_CALLBACK;
  711. break;
  712. }
  713. }
  714. else
  715. code = CURLE_OK;
  716. return code;
  717. }
  718. #endif