tftp.c 26 KB

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