tftp.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497
  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. /* No, log it */
  506. infof(data,
  507. "Received unexpected DATA packet block %d, expecting block %d\n",
  508. rblock, NEXT_BLOCKNUM(state->block));
  509. break;
  510. }
  511. /* This is the expected block. Reset counters and ACK it. */
  512. state->block = (unsigned short)rblock;
  513. state->retries = 0;
  514. setpacketevent(&state->spacket, TFTP_EVENT_ACK);
  515. setpacketblock(&state->spacket, state->block);
  516. sbytes = sendto(state->sockfd, (void *)state->spacket.data,
  517. 4, SEND_4TH_ARG,
  518. (struct sockaddr *)&state->remote_addr,
  519. state->remote_addrlen);
  520. if(sbytes < 0) {
  521. failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO));
  522. return CURLE_SEND_ERROR;
  523. }
  524. /* Check if completed (That is, a less than full packet is received) */
  525. if(state->rbytes < (ssize_t)state->blksize+4) {
  526. state->state = TFTP_STATE_FIN;
  527. }
  528. else {
  529. state->state = TFTP_STATE_RX;
  530. }
  531. time(&state->rx_time);
  532. break;
  533. case TFTP_EVENT_OACK:
  534. /* ACK option acknowledgement so we can move on to data */
  535. state->block = 0;
  536. state->retries = 0;
  537. setpacketevent(&state->spacket, TFTP_EVENT_ACK);
  538. setpacketblock(&state->spacket, state->block);
  539. sbytes = sendto(state->sockfd, (void *)state->spacket.data,
  540. 4, SEND_4TH_ARG,
  541. (struct sockaddr *)&state->remote_addr,
  542. state->remote_addrlen);
  543. if(sbytes < 0) {
  544. failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO));
  545. return CURLE_SEND_ERROR;
  546. }
  547. /* we're ready to RX data */
  548. state->state = TFTP_STATE_RX;
  549. time(&state->rx_time);
  550. break;
  551. case TFTP_EVENT_TIMEOUT:
  552. /* Increment the retry count and fail if over the limit */
  553. state->retries++;
  554. infof(data,
  555. "Timeout waiting for block %d ACK. Retries = %d\n",
  556. NEXT_BLOCKNUM(state->block), state->retries);
  557. if(state->retries > state->retry_max) {
  558. state->error = TFTP_ERR_TIMEOUT;
  559. state->state = TFTP_STATE_FIN;
  560. }
  561. else {
  562. /* Resend the previous ACK */
  563. sbytes = sendto(state->sockfd, (void *)state->spacket.data,
  564. 4, SEND_4TH_ARG,
  565. (struct sockaddr *)&state->remote_addr,
  566. state->remote_addrlen);
  567. if(sbytes<0) {
  568. failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO));
  569. return CURLE_SEND_ERROR;
  570. }
  571. }
  572. break;
  573. case TFTP_EVENT_ERROR:
  574. setpacketevent(&state->spacket, TFTP_EVENT_ERROR);
  575. setpacketblock(&state->spacket, state->block);
  576. (void)sendto(state->sockfd, (void *)state->spacket.data,
  577. 4, SEND_4TH_ARG,
  578. (struct sockaddr *)&state->remote_addr,
  579. state->remote_addrlen);
  580. /* don't bother with the return code, but if the socket is still up we
  581. * should be a good TFTP client and let the server know we're done */
  582. state->state = TFTP_STATE_FIN;
  583. break;
  584. default:
  585. failf(data, "%s", "tftp_rx: internal error");
  586. return CURLE_TFTP_ILLEGAL; /* not really the perfect return code for
  587. this */
  588. }
  589. return CURLE_OK;
  590. }
  591. /**********************************************************
  592. *
  593. * tftp_tx
  594. *
  595. * Event handler for the TX state
  596. *
  597. **********************************************************/
  598. static CURLcode tftp_tx(tftp_state_data_t *state, tftp_event_t event)
  599. {
  600. struct SessionHandle *data = state->conn->data;
  601. ssize_t sbytes;
  602. int rblock;
  603. CURLcode res = CURLE_OK;
  604. struct SingleRequest *k = &data->req;
  605. switch(event) {
  606. case TFTP_EVENT_ACK:
  607. case TFTP_EVENT_OACK:
  608. if(event == TFTP_EVENT_ACK) {
  609. /* Ack the packet */
  610. rblock = getrpacketblock(&state->rpacket);
  611. if(rblock != state->block &&
  612. /* There's a bug in tftpd-hpa that causes it to send us an ack for
  613. * 65535 when the block number wraps to 0. So when we're expecting
  614. * 0, also accept 65535. See
  615. * http://syslinux.zytor.com/archives/2010-September/015253.html
  616. * */
  617. !(state->block == 0 && rblock == 65535)) {
  618. /* This isn't the expected block. Log it and up the retry counter */
  619. infof(data, "Received ACK for block %d, expecting %d\n",
  620. rblock, state->block);
  621. state->retries++;
  622. /* Bail out if over the maximum */
  623. if(state->retries>state->retry_max) {
  624. failf(data, "tftp_tx: giving up waiting for block %d ack",
  625. state->block);
  626. res = CURLE_SEND_ERROR;
  627. }
  628. else {
  629. /* Re-send the data packet */
  630. sbytes = sendto(state->sockfd, (void *)state->spacket.data,
  631. 4+state->sbytes, SEND_4TH_ARG,
  632. (struct sockaddr *)&state->remote_addr,
  633. state->remote_addrlen);
  634. /* Check all sbytes were sent */
  635. if(sbytes<0) {
  636. failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO));
  637. res = CURLE_SEND_ERROR;
  638. }
  639. }
  640. return res;
  641. }
  642. /* This is the expected packet. Reset the counters and send the next
  643. block */
  644. time(&state->rx_time);
  645. state->block++;
  646. }
  647. else
  648. state->block = 1; /* first data block is 1 when using OACK */
  649. state->retries = 0;
  650. setpacketevent(&state->spacket, TFTP_EVENT_DATA);
  651. setpacketblock(&state->spacket, state->block);
  652. if(state->block > 1 && state->sbytes < (int)state->blksize) {
  653. state->state = TFTP_STATE_FIN;
  654. return CURLE_OK;
  655. }
  656. res = Curl_fillreadbuffer(state->conn, state->blksize, &state->sbytes);
  657. if(res)
  658. return res;
  659. sbytes = sendto(state->sockfd, (void *)state->spacket.data,
  660. 4+state->sbytes, SEND_4TH_ARG,
  661. (struct sockaddr *)&state->remote_addr,
  662. state->remote_addrlen);
  663. /* Check all sbytes were sent */
  664. if(sbytes<0) {
  665. failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO));
  666. return CURLE_SEND_ERROR;
  667. }
  668. /* Update the progress meter */
  669. k->writebytecount += state->sbytes;
  670. Curl_pgrsSetUploadCounter(data, k->writebytecount);
  671. break;
  672. case TFTP_EVENT_TIMEOUT:
  673. /* Increment the retry counter and log the timeout */
  674. state->retries++;
  675. infof(data, "Timeout waiting for block %d ACK. "
  676. " Retries = %d\n", NEXT_BLOCKNUM(state->block), state->retries);
  677. /* Decide if we've had enough */
  678. if(state->retries > state->retry_max) {
  679. state->error = TFTP_ERR_TIMEOUT;
  680. state->state = TFTP_STATE_FIN;
  681. }
  682. else {
  683. /* Re-send the data packet */
  684. sbytes = sendto(state->sockfd, (void *)state->spacket.data,
  685. 4+state->sbytes, SEND_4TH_ARG,
  686. (struct sockaddr *)&state->remote_addr,
  687. state->remote_addrlen);
  688. /* Check all sbytes were sent */
  689. if(sbytes<0) {
  690. failf(data, "%s", Curl_strerror(state->conn, SOCKERRNO));
  691. return CURLE_SEND_ERROR;
  692. }
  693. /* since this was a re-send, we remain at the still byte position */
  694. Curl_pgrsSetUploadCounter(data, k->writebytecount);
  695. }
  696. break;
  697. case TFTP_EVENT_ERROR:
  698. state->state = TFTP_STATE_FIN;
  699. setpacketevent(&state->spacket, TFTP_EVENT_ERROR);
  700. setpacketblock(&state->spacket, state->block);
  701. (void)sendto(state->sockfd, (void *)state->spacket.data, 4, SEND_4TH_ARG,
  702. (struct sockaddr *)&state->remote_addr,
  703. state->remote_addrlen);
  704. /* don't bother with the return code, but if the socket is still up we
  705. * should be a good TFTP client and let the server know we're done */
  706. state->state = TFTP_STATE_FIN;
  707. break;
  708. default:
  709. failf(data, "tftp_tx: internal error, event: %i", (int)(event));
  710. break;
  711. }
  712. return res;
  713. }
  714. /**********************************************************
  715. *
  716. * tftp_translate_code
  717. *
  718. * Translate internal error codes to CURL error codes
  719. *
  720. **********************************************************/
  721. static CURLcode tftp_translate_code(tftp_error_t error)
  722. {
  723. CURLcode code = CURLE_OK;
  724. if(error != TFTP_ERR_NONE) {
  725. switch(error) {
  726. case TFTP_ERR_NOTFOUND:
  727. code = CURLE_TFTP_NOTFOUND;
  728. break;
  729. case TFTP_ERR_PERM:
  730. code = CURLE_TFTP_PERM;
  731. break;
  732. case TFTP_ERR_DISKFULL:
  733. code = CURLE_REMOTE_DISK_FULL;
  734. break;
  735. case TFTP_ERR_UNDEF:
  736. case TFTP_ERR_ILLEGAL:
  737. code = CURLE_TFTP_ILLEGAL;
  738. break;
  739. case TFTP_ERR_UNKNOWNID:
  740. code = CURLE_TFTP_UNKNOWNID;
  741. break;
  742. case TFTP_ERR_EXISTS:
  743. code = CURLE_REMOTE_FILE_EXISTS;
  744. break;
  745. case TFTP_ERR_NOSUCHUSER:
  746. code = CURLE_TFTP_NOSUCHUSER;
  747. break;
  748. case TFTP_ERR_TIMEOUT:
  749. code = CURLE_OPERATION_TIMEDOUT;
  750. break;
  751. case TFTP_ERR_NORESPONSE:
  752. code = CURLE_COULDNT_CONNECT;
  753. break;
  754. default:
  755. code= CURLE_ABORTED_BY_CALLBACK;
  756. break;
  757. }
  758. }
  759. else {
  760. code = CURLE_OK;
  761. }
  762. return(code);
  763. }
  764. /**********************************************************
  765. *
  766. * tftp_state_machine
  767. *
  768. * The tftp state machine event dispatcher
  769. *
  770. **********************************************************/
  771. static CURLcode tftp_state_machine(tftp_state_data_t *state,
  772. tftp_event_t event)
  773. {
  774. CURLcode res = CURLE_OK;
  775. struct SessionHandle *data = state->conn->data;
  776. switch(state->state) {
  777. case TFTP_STATE_START:
  778. DEBUGF(infof(data, "TFTP_STATE_START\n"));
  779. res = tftp_send_first(state, event);
  780. break;
  781. case TFTP_STATE_RX:
  782. DEBUGF(infof(data, "TFTP_STATE_RX\n"));
  783. res = tftp_rx(state, event);
  784. break;
  785. case TFTP_STATE_TX:
  786. DEBUGF(infof(data, "TFTP_STATE_TX\n"));
  787. res = tftp_tx(state, event);
  788. break;
  789. case TFTP_STATE_FIN:
  790. infof(data, "%s\n", "TFTP finished");
  791. break;
  792. default:
  793. DEBUGF(infof(data, "STATE: %d\n", state->state));
  794. failf(data, "%s", "Internal state machine error");
  795. res = CURLE_TFTP_ILLEGAL;
  796. break;
  797. }
  798. return res;
  799. }
  800. /**********************************************************
  801. *
  802. * tftp_disconnect
  803. *
  804. * The disconnect callback
  805. *
  806. **********************************************************/
  807. static CURLcode tftp_disconnect(struct connectdata *conn, bool dead_connection)
  808. {
  809. tftp_state_data_t *state = conn->proto.tftpc;
  810. (void) dead_connection;
  811. /* done, free dynamically allocated pkt buffers */
  812. if(state) {
  813. Curl_safefree(state->rpacket.data);
  814. Curl_safefree(state->spacket.data);
  815. free(state);
  816. }
  817. return CURLE_OK;
  818. }
  819. /**********************************************************
  820. *
  821. * tftp_connect
  822. *
  823. * The connect callback
  824. *
  825. **********************************************************/
  826. static CURLcode tftp_connect(struct connectdata *conn, bool *done)
  827. {
  828. CURLcode code;
  829. tftp_state_data_t *state;
  830. int blksize, rc;
  831. blksize = TFTP_BLKSIZE_DEFAULT;
  832. /* If there already is a protocol-specific struct allocated for this
  833. sessionhandle, deal with it */
  834. Curl_reset_reqproto(conn);
  835. state = conn->proto.tftpc = calloc(1, sizeof(tftp_state_data_t));
  836. if(!state)
  837. return CURLE_OUT_OF_MEMORY;
  838. /* alloc pkt buffers based on specified blksize */
  839. if(conn->data->set.tftp_blksize) {
  840. blksize = (int)conn->data->set.tftp_blksize;
  841. if(blksize > TFTP_BLKSIZE_MAX || blksize < TFTP_BLKSIZE_MIN )
  842. return CURLE_TFTP_ILLEGAL;
  843. }
  844. if(!state->rpacket.data) {
  845. state->rpacket.data = calloc(1, blksize + 2 + 2);
  846. if(!state->rpacket.data)
  847. return CURLE_OUT_OF_MEMORY;
  848. }
  849. if(!state->spacket.data) {
  850. state->spacket.data = calloc(1, blksize + 2 + 2);
  851. if(!state->spacket.data)
  852. return CURLE_OUT_OF_MEMORY;
  853. }
  854. conn->bits.close = TRUE; /* we don't keep TFTP connections up bascially
  855. because there's none or very little gain for UDP
  856. */
  857. state->conn = conn;
  858. state->sockfd = state->conn->sock[FIRSTSOCKET];
  859. state->state = TFTP_STATE_START;
  860. state->error = TFTP_ERR_NONE;
  861. state->blksize = TFTP_BLKSIZE_DEFAULT;
  862. state->requested_blksize = blksize;
  863. ((struct sockaddr *)&state->local_addr)->sa_family =
  864. (unsigned short)(conn->ip_addr->ai_family);
  865. tftp_set_timeouts(state);
  866. if(!conn->bits.bound) {
  867. /* If not already bound, bind to any interface, random UDP port. If it is
  868. * reused or a custom local port was desired, this has already been done!
  869. *
  870. * We once used the size of the local_addr struct as the third argument
  871. * for bind() to better work with IPv6 or whatever size the struct could
  872. * have, but we learned that at least Tru64, AIX and IRIX *requires* the
  873. * size of that argument to match the exact size of a 'sockaddr_in' struct
  874. * when running IPv4-only.
  875. *
  876. * Therefore we use the size from the address we connected to, which we
  877. * assume uses the same IP version and thus hopefully this works for both
  878. * IPv4 and IPv6...
  879. */
  880. rc = bind(state->sockfd, (struct sockaddr *)&state->local_addr,
  881. conn->ip_addr->ai_addrlen);
  882. if(rc) {
  883. failf(conn->data, "bind() failed; %s",
  884. Curl_strerror(conn, SOCKERRNO));
  885. return CURLE_COULDNT_CONNECT;
  886. }
  887. conn->bits.bound = TRUE;
  888. }
  889. Curl_pgrsStartNow(conn->data);
  890. *done = TRUE;
  891. code = CURLE_OK;
  892. return(code);
  893. }
  894. /**********************************************************
  895. *
  896. * tftp_done
  897. *
  898. * The done callback
  899. *
  900. **********************************************************/
  901. static CURLcode tftp_done(struct connectdata *conn, CURLcode status,
  902. bool premature)
  903. {
  904. CURLcode code = CURLE_OK;
  905. tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc;
  906. (void)status; /* unused */
  907. (void)premature; /* not used */
  908. if(Curl_pgrsDone(conn))
  909. return CURLE_ABORTED_BY_CALLBACK;
  910. /* If we have encountered an error */
  911. code = tftp_translate_code(state->error);
  912. return code;
  913. }
  914. /**********************************************************
  915. *
  916. * tftp_getsock
  917. *
  918. * The getsock callback
  919. *
  920. **********************************************************/
  921. static int tftp_getsock(struct connectdata *conn, curl_socket_t *socks,
  922. int numsocks)
  923. {
  924. if(!numsocks)
  925. return GETSOCK_BLANK;
  926. socks[0] = conn->sock[FIRSTSOCKET];
  927. return GETSOCK_READSOCK(0);
  928. }
  929. /**********************************************************
  930. *
  931. * tftp_receive_packet
  932. *
  933. * Called once select fires and data is ready on the socket
  934. *
  935. **********************************************************/
  936. static CURLcode tftp_receive_packet(struct connectdata *conn)
  937. {
  938. struct Curl_sockaddr_storage fromaddr;
  939. curl_socklen_t fromlen;
  940. CURLcode result = CURLE_OK;
  941. struct SessionHandle *data = conn->data;
  942. tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc;
  943. struct SingleRequest *k = &data->req;
  944. /* Receive the packet */
  945. fromlen = sizeof(fromaddr);
  946. state->rbytes = (int)recvfrom(state->sockfd,
  947. (void *)state->rpacket.data,
  948. state->blksize+4,
  949. 0,
  950. (struct sockaddr *)&fromaddr,
  951. &fromlen);
  952. if(state->remote_addrlen==0) {
  953. memcpy(&state->remote_addr, &fromaddr, fromlen);
  954. state->remote_addrlen = fromlen;
  955. }
  956. /* Sanity check packet length */
  957. if(state->rbytes < 4) {
  958. failf(data, "Received too short packet");
  959. /* Not a timeout, but how best to handle it? */
  960. state->event = TFTP_EVENT_TIMEOUT;
  961. }
  962. else {
  963. /* The event is given by the TFTP packet time */
  964. state->event = (tftp_event_t)getrpacketevent(&state->rpacket);
  965. switch(state->event) {
  966. case TFTP_EVENT_DATA:
  967. /* Don't pass to the client empty or retransmitted packets */
  968. if(state->rbytes > 4 &&
  969. (NEXT_BLOCKNUM(state->block) == getrpacketblock(&state->rpacket))) {
  970. result = Curl_client_write(conn, CLIENTWRITE_BODY,
  971. (char *)state->rpacket.data+4,
  972. state->rbytes-4);
  973. if(result) {
  974. tftp_state_machine(state, TFTP_EVENT_ERROR);
  975. return result;
  976. }
  977. k->bytecount += state->rbytes-4;
  978. Curl_pgrsSetDownloadCounter(data, (curl_off_t) k->bytecount);
  979. }
  980. break;
  981. case TFTP_EVENT_ERROR:
  982. state->error = (tftp_error_t)getrpacketblock(&state->rpacket);
  983. infof(data, "%s\n", (const char *)state->rpacket.data+4);
  984. break;
  985. case TFTP_EVENT_ACK:
  986. break;
  987. case TFTP_EVENT_OACK:
  988. result = tftp_parse_option_ack(state,
  989. (const char *)state->rpacket.data+2,
  990. state->rbytes-2);
  991. if(result)
  992. return result;
  993. break;
  994. case TFTP_EVENT_RRQ:
  995. case TFTP_EVENT_WRQ:
  996. default:
  997. failf(data, "%s", "Internal error: Unexpected packet");
  998. break;
  999. }
  1000. /* Update the progress meter */
  1001. if(Curl_pgrsUpdate(conn)) {
  1002. tftp_state_machine(state, TFTP_EVENT_ERROR);
  1003. return CURLE_ABORTED_BY_CALLBACK;
  1004. }
  1005. }
  1006. return result;
  1007. }
  1008. /**********************************************************
  1009. *
  1010. * tftp_state_timeout
  1011. *
  1012. * Check if timeouts have been reached
  1013. *
  1014. **********************************************************/
  1015. static long tftp_state_timeout(struct connectdata *conn, tftp_event_t *event)
  1016. {
  1017. time_t current;
  1018. tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc;
  1019. if(event)
  1020. *event = TFTP_EVENT_NONE;
  1021. time(&current);
  1022. if(current > state->max_time) {
  1023. DEBUGF(infof(conn->data, "timeout: %ld > %ld\n",
  1024. (long)current, (long)state->max_time));
  1025. state->error = TFTP_ERR_TIMEOUT;
  1026. state->state = TFTP_STATE_FIN;
  1027. return 0;
  1028. }
  1029. else if(current > state->rx_time+state->retry_time) {
  1030. if(event)
  1031. *event = TFTP_EVENT_TIMEOUT;
  1032. time(&state->rx_time); /* update even though we received nothing */
  1033. }
  1034. /* there's a typecast below here since 'time_t' may in fact be larger than
  1035. 'long', but we estimate that a 'long' will still be able to hold number
  1036. of seconds even if "only" 32 bit */
  1037. return (long)(state->max_time - current);
  1038. }
  1039. /**********************************************************
  1040. *
  1041. * tftp_easy_statemach
  1042. *
  1043. * Handle easy request until completion
  1044. *
  1045. **********************************************************/
  1046. static CURLcode tftp_easy_statemach(struct connectdata *conn)
  1047. {
  1048. int rc;
  1049. int check_time = 0;
  1050. CURLcode result = CURLE_OK;
  1051. struct SessionHandle *data = conn->data;
  1052. tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc;
  1053. curl_socket_t fd_read;
  1054. long timeout_ms;
  1055. struct SingleRequest *k = &data->req;
  1056. struct timeval transaction_start = Curl_tvnow();
  1057. k->start = transaction_start;
  1058. k->now = transaction_start;
  1059. /* Run the TFTP State Machine */
  1060. for(; (state->state != TFTP_STATE_FIN) && (result == CURLE_OK); ) {
  1061. timeout_ms = state->retry_time * 1000;
  1062. if(data->set.upload) {
  1063. if(data->set.max_send_speed &&
  1064. (data->progress.ulspeed > data->set.max_send_speed)) {
  1065. fd_read = CURL_SOCKET_BAD;
  1066. timeout_ms = Curl_sleep_time(data->set.max_send_speed,
  1067. data->progress.ulspeed, state->blksize);
  1068. }
  1069. else {
  1070. fd_read = state->sockfd;
  1071. }
  1072. }
  1073. else {
  1074. if(data->set.max_recv_speed &&
  1075. (data->progress.dlspeed > data->set.max_recv_speed)) {
  1076. fd_read = CURL_SOCKET_BAD;
  1077. timeout_ms = Curl_sleep_time(data->set.max_recv_speed,
  1078. data->progress.dlspeed, state->blksize);
  1079. }
  1080. else
  1081. fd_read = state->sockfd;
  1082. }
  1083. if(data->set.timeout) {
  1084. timeout_ms = data->set.timeout - Curl_tvdiff(k->now, k->start);
  1085. if(timeout_ms > state->retry_time * 1000)
  1086. timeout_ms = state->retry_time * 1000;
  1087. else if(timeout_ms < 0)
  1088. timeout_ms = 0;
  1089. }
  1090. /* Wait until ready to read or timeout occurs */
  1091. rc = Curl_socket_ready(fd_read, CURL_SOCKET_BAD, timeout_ms);
  1092. k->now = Curl_tvnow();
  1093. /* Force a progress callback if it's been too long */
  1094. if(Curl_tvdiff(k->now, k->start) >= data->set.timeout) {
  1095. if(Curl_pgrsUpdate(conn)) {
  1096. tftp_state_machine(state, TFTP_EVENT_ERROR);
  1097. return CURLE_ABORTED_BY_CALLBACK;
  1098. }
  1099. k->start = k->now;
  1100. }
  1101. if(rc == -1) {
  1102. /* bail out */
  1103. int error = SOCKERRNO;
  1104. failf(data, "%s", Curl_strerror(conn, error));
  1105. state->event = TFTP_EVENT_ERROR;
  1106. }
  1107. else {
  1108. if(rc==0) {
  1109. /* A timeout occurred, but our timeout is variable, so maybe
  1110. just continue? */
  1111. long rtms = state->retry_time * 1000;
  1112. if(Curl_tvdiff(k->now, transaction_start) > rtms) {
  1113. state->event = TFTP_EVENT_TIMEOUT;
  1114. /* Force a look at transfer timeouts */
  1115. check_time = 1;
  1116. }
  1117. else {
  1118. continue; /* skip state machine */
  1119. }
  1120. }
  1121. else {
  1122. result = tftp_receive_packet(conn);
  1123. if(result == CURLE_OK)
  1124. transaction_start = Curl_tvnow();
  1125. if(k->bytecountp)
  1126. *k->bytecountp = k->bytecount; /* read count */
  1127. if(k->writebytecountp)
  1128. *k->writebytecountp = k->writebytecount; /* write count */
  1129. }
  1130. }
  1131. if(check_time) {
  1132. tftp_state_timeout(conn, NULL);
  1133. check_time = 0;
  1134. }
  1135. if(result)
  1136. return(result);
  1137. result = tftp_state_machine(state, state->event);
  1138. }
  1139. /* Tell curl we're done */
  1140. Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
  1141. return(result);
  1142. }
  1143. /**********************************************************
  1144. *
  1145. * tftp_multi_statemach
  1146. *
  1147. * Handle single RX socket event and return
  1148. *
  1149. **********************************************************/
  1150. static CURLcode tftp_multi_statemach(struct connectdata *conn, bool *done)
  1151. {
  1152. int rc;
  1153. tftp_event_t event;
  1154. CURLcode result = CURLE_OK;
  1155. struct SessionHandle *data = conn->data;
  1156. tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc;
  1157. long timeout_ms = tftp_state_timeout(conn, &event);
  1158. *done = FALSE;
  1159. if(timeout_ms <= 0) {
  1160. failf(data, "TFTP response timeout");
  1161. return CURLE_OPERATION_TIMEDOUT;
  1162. }
  1163. else if(event != TFTP_EVENT_NONE) {
  1164. result = tftp_state_machine(state, event);
  1165. if(result != CURLE_OK)
  1166. return(result);
  1167. *done = (state->state == TFTP_STATE_FIN) ? TRUE : FALSE;
  1168. if(*done)
  1169. /* Tell curl we're done */
  1170. Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
  1171. }
  1172. else {
  1173. /* no timeouts to handle, check our socket */
  1174. rc = Curl_socket_ready(state->sockfd, CURL_SOCKET_BAD, 0);
  1175. if(rc == -1) {
  1176. /* bail out */
  1177. int error = SOCKERRNO;
  1178. failf(data, "%s", Curl_strerror(conn, error));
  1179. state->event = TFTP_EVENT_ERROR;
  1180. }
  1181. else if(rc != 0) {
  1182. result = tftp_receive_packet(conn);
  1183. if(result != CURLE_OK)
  1184. return(result);
  1185. result = tftp_state_machine(state, state->event);
  1186. if(result != CURLE_OK)
  1187. return(result);
  1188. *done = (state->state == TFTP_STATE_FIN) ? TRUE : FALSE;
  1189. if(*done)
  1190. /* Tell curl we're done */
  1191. Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
  1192. }
  1193. /* if rc == 0, then select() timed out */
  1194. }
  1195. return result;
  1196. }
  1197. /**********************************************************
  1198. *
  1199. * tftp_doing
  1200. *
  1201. * Called from multi.c while DOing
  1202. *
  1203. **********************************************************/
  1204. static CURLcode tftp_doing(struct connectdata *conn, bool *dophase_done)
  1205. {
  1206. CURLcode result;
  1207. result = tftp_multi_statemach(conn, dophase_done);
  1208. if(*dophase_done) {
  1209. DEBUGF(infof(conn->data, "DO phase is complete\n"));
  1210. }
  1211. return result;
  1212. }
  1213. /**********************************************************
  1214. *
  1215. * tftp_peform
  1216. *
  1217. * Entry point for transfer from tftp_do, sarts state mach
  1218. *
  1219. **********************************************************/
  1220. static CURLcode tftp_perform(struct connectdata *conn, bool *dophase_done)
  1221. {
  1222. CURLcode result = CURLE_OK;
  1223. tftp_state_data_t *state = (tftp_state_data_t *)conn->proto.tftpc;
  1224. *dophase_done = FALSE;
  1225. result = tftp_state_machine(state, TFTP_EVENT_INIT);
  1226. if(state->state == TFTP_STATE_FIN || result != CURLE_OK)
  1227. return(result);
  1228. if(conn->data->state.used_interface == Curl_if_multi)
  1229. tftp_multi_statemach(conn, dophase_done);
  1230. else {
  1231. result = tftp_easy_statemach(conn);
  1232. *dophase_done = TRUE; /* with the easy interface we are done here */
  1233. }
  1234. if(*dophase_done)
  1235. DEBUGF(infof(conn->data, "DO phase is complete\n"));
  1236. return result;
  1237. }
  1238. /**********************************************************
  1239. *
  1240. * tftp_do
  1241. *
  1242. * The do callback
  1243. *
  1244. * This callback initiates the TFTP transfer
  1245. *
  1246. **********************************************************/
  1247. static CURLcode tftp_do(struct connectdata *conn, bool *done)
  1248. {
  1249. tftp_state_data_t *state;
  1250. CURLcode code;
  1251. *done = FALSE;
  1252. /*
  1253. Since connections can be re-used between SessionHandles, this might be a
  1254. connection already existing but on a fresh SessionHandle struct so we must
  1255. make sure we have a good 'struct TFTP' to play with. For new connections,
  1256. the struct TFTP is allocated and setup in the tftp_connect() function.
  1257. */
  1258. Curl_reset_reqproto(conn);
  1259. if(!conn->proto.tftpc) {
  1260. code = tftp_connect(conn, done);
  1261. if(code)
  1262. return code;
  1263. }
  1264. state = (tftp_state_data_t *)conn->proto.tftpc;
  1265. code = tftp_perform(conn, done);
  1266. /* If tftp_perform() returned an error, use that for return code. If it
  1267. was OK, see if tftp_translate_code() has an error. */
  1268. if(code == CURLE_OK)
  1269. /* If we have encountered an internal tftp error, translate it. */
  1270. code = tftp_translate_code(state->error);
  1271. return code;
  1272. }
  1273. static CURLcode tftp_setup_connection(struct connectdata * conn)
  1274. {
  1275. struct SessionHandle *data = conn->data;
  1276. char * type;
  1277. char command;
  1278. conn->socktype = SOCK_DGRAM; /* UDP datagram based */
  1279. /* TFTP URLs support an extension like ";mode=<typecode>" that
  1280. * we'll try to get now! */
  1281. type = strstr(data->state.path, ";mode=");
  1282. if(!type)
  1283. type = strstr(conn->host.rawalloc, ";mode=");
  1284. if(type) {
  1285. *type = 0; /* it was in the middle of the hostname */
  1286. command = Curl_raw_toupper(type[6]);
  1287. switch (command) {
  1288. case 'A': /* ASCII mode */
  1289. case 'N': /* NETASCII mode */
  1290. data->set.prefer_ascii = TRUE;
  1291. break;
  1292. case 'O': /* octet mode */
  1293. case 'I': /* binary mode */
  1294. default:
  1295. /* switch off ASCII */
  1296. data->set.prefer_ascii = FALSE;
  1297. break;
  1298. }
  1299. }
  1300. return CURLE_OK;
  1301. }
  1302. #endif