tftp.c 44 KB

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