tftp.c 41 KB

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