tftp.c 41 KB

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