2
0

tftp.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  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);
  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. msnprintf(buf, sizeof(buf), "%" FMT_OFF_T,
  416. data->state.upload && (data->state.infilesize != -1) ?
  417. data->state.infilesize : 0);
  418. result = tftp_option_add(state, &sbytes,
  419. (char *)state->spacket.data + sbytes,
  420. TFTP_OPTION_TSIZE);
  421. if(result == CURLE_OK)
  422. result = tftp_option_add(state, &sbytes,
  423. (char *)state->spacket.data + sbytes, buf);
  424. /* add blksize option */
  425. msnprintf(buf, sizeof(buf), "%d", state->requested_blksize);
  426. if(result == CURLE_OK)
  427. result = tftp_option_add(state, &sbytes,
  428. (char *)state->spacket.data + sbytes,
  429. TFTP_OPTION_BLKSIZE);
  430. if(result == CURLE_OK)
  431. result = tftp_option_add(state, &sbytes,
  432. (char *)state->spacket.data + sbytes, buf);
  433. /* add timeout option */
  434. msnprintf(buf, sizeof(buf), "%d", state->retry_time);
  435. if(result == CURLE_OK)
  436. result = tftp_option_add(state, &sbytes,
  437. (char *)state->spacket.data + sbytes,
  438. TFTP_OPTION_INTERVAL);
  439. if(result == CURLE_OK)
  440. result = tftp_option_add(state, &sbytes,
  441. (char *)state->spacket.data + sbytes, buf);
  442. if(result != CURLE_OK) {
  443. failf(data, "TFTP buffer too small for options");
  444. free(filename);
  445. return CURLE_TFTP_ILLEGAL;
  446. }
  447. }
  448. /* the typecase for the 3rd argument is mostly for systems that do
  449. not have a size_t argument, like older unixes that want an 'int' */
  450. senddata = sendto(state->sockfd, (void *)state->spacket.data,
  451. (SEND_TYPE_ARG3)sbytes, 0,
  452. &data->conn->remote_addr->curl_sa_addr,
  453. (curl_socklen_t)data->conn->remote_addr->addrlen);
  454. if(senddata != (ssize_t)sbytes) {
  455. char buffer[STRERROR_LEN];
  456. failf(data, "%s", Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
  457. }
  458. free(filename);
  459. break;
  460. case TFTP_EVENT_OACK:
  461. if(data->state.upload) {
  462. result = tftp_connect_for_tx(state, event);
  463. }
  464. else {
  465. result = tftp_connect_for_rx(state, event);
  466. }
  467. break;
  468. case TFTP_EVENT_ACK: /* Connected for transmit */
  469. result = tftp_connect_for_tx(state, event);
  470. break;
  471. case TFTP_EVENT_DATA: /* Connected for receive */
  472. result = tftp_connect_for_rx(state, event);
  473. break;
  474. case TFTP_EVENT_ERROR:
  475. state->state = TFTP_STATE_FIN;
  476. break;
  477. default:
  478. failf(state->data, "tftp_send_first: internal error");
  479. break;
  480. }
  481. return result;
  482. }
  483. /* the next blocknum is x + 1 but it needs to wrap at an unsigned 16bit
  484. boundary */
  485. #define NEXT_BLOCKNUM(x) (((x) + 1)&0xffff)
  486. /**********************************************************
  487. *
  488. * tftp_rx
  489. *
  490. * Event handler for the RX state
  491. *
  492. **********************************************************/
  493. static CURLcode tftp_rx(struct tftp_state_data *state,
  494. tftp_event_t event)
  495. {
  496. ssize_t sbytes;
  497. int rblock;
  498. struct Curl_easy *data = state->data;
  499. char buffer[STRERROR_LEN];
  500. switch(event) {
  501. case TFTP_EVENT_DATA:
  502. /* Is this the block we expect? */
  503. rblock = getrpacketblock(&state->rpacket);
  504. if(NEXT_BLOCKNUM(state->block) == rblock) {
  505. /* This is the expected block. Reset counters and ACK it. */
  506. state->retries = 0;
  507. }
  508. else if(state->block == rblock) {
  509. /* This is the last recently received block again. Log it and ACK it
  510. again. */
  511. infof(data, "Received last DATA packet block %d again.", rblock);
  512. }
  513. else {
  514. /* totally unexpected, just log it */
  515. infof(data,
  516. "Received unexpected DATA packet block %d, expecting block %d",
  517. rblock, NEXT_BLOCKNUM(state->block));
  518. break;
  519. }
  520. /* ACK this block. */
  521. state->block = (unsigned short)rblock;
  522. setpacketevent(&state->spacket, TFTP_EVENT_ACK);
  523. setpacketblock(&state->spacket, state->block);
  524. sbytes = sendto(state->sockfd, (void *)state->spacket.data,
  525. 4, SEND_4TH_ARG,
  526. (struct sockaddr *)&state->remote_addr,
  527. state->remote_addrlen);
  528. if(sbytes < 0) {
  529. failf(data, "%s", Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
  530. return CURLE_SEND_ERROR;
  531. }
  532. /* Check if completed (That is, a less than full packet is received) */
  533. if(state->rbytes < (ssize_t)state->blksize + 4) {
  534. state->state = TFTP_STATE_FIN;
  535. }
  536. else {
  537. state->state = TFTP_STATE_RX;
  538. }
  539. state->rx_time = time(NULL);
  540. break;
  541. case TFTP_EVENT_OACK:
  542. /* ACK option acknowledgement so we can move on to data */
  543. state->block = 0;
  544. state->retries = 0;
  545. setpacketevent(&state->spacket, TFTP_EVENT_ACK);
  546. setpacketblock(&state->spacket, state->block);
  547. sbytes = sendto(state->sockfd, (void *)state->spacket.data,
  548. 4, SEND_4TH_ARG,
  549. (struct sockaddr *)&state->remote_addr,
  550. state->remote_addrlen);
  551. if(sbytes < 0) {
  552. failf(data, "%s", Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
  553. return CURLE_SEND_ERROR;
  554. }
  555. /* we are ready to RX data */
  556. state->state = TFTP_STATE_RX;
  557. state->rx_time = time(NULL);
  558. break;
  559. case TFTP_EVENT_TIMEOUT:
  560. /* Increment the retry count and fail if over the limit */
  561. state->retries++;
  562. infof(data,
  563. "Timeout waiting for block %d ACK. Retries = %d",
  564. NEXT_BLOCKNUM(state->block), state->retries);
  565. if(state->retries > state->retry_max) {
  566. state->error = TFTP_ERR_TIMEOUT;
  567. state->state = TFTP_STATE_FIN;
  568. }
  569. else {
  570. /* Resend the previous ACK */
  571. sbytes = sendto(state->sockfd, (void *)state->spacket.data,
  572. 4, SEND_4TH_ARG,
  573. (struct sockaddr *)&state->remote_addr,
  574. state->remote_addrlen);
  575. if(sbytes < 0) {
  576. failf(data, "%s", Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
  577. return CURLE_SEND_ERROR;
  578. }
  579. }
  580. break;
  581. case TFTP_EVENT_ERROR:
  582. setpacketevent(&state->spacket, TFTP_EVENT_ERROR);
  583. setpacketblock(&state->spacket, state->block);
  584. (void)sendto(state->sockfd, (void *)state->spacket.data,
  585. 4, SEND_4TH_ARG,
  586. (struct sockaddr *)&state->remote_addr,
  587. state->remote_addrlen);
  588. /* do not bother with the return code, but if the socket is still up we
  589. * should be a good TFTP client and let the server know we are done */
  590. state->state = TFTP_STATE_FIN;
  591. break;
  592. default:
  593. failf(data, "%s", "tftp_rx: internal error");
  594. return CURLE_TFTP_ILLEGAL; /* not really the perfect return code for
  595. this */
  596. }
  597. return CURLE_OK;
  598. }
  599. /**********************************************************
  600. *
  601. * tftp_tx
  602. *
  603. * Event handler for the TX state
  604. *
  605. **********************************************************/
  606. static CURLcode tftp_tx(struct tftp_state_data *state, tftp_event_t event)
  607. {
  608. struct Curl_easy *data = state->data;
  609. ssize_t sbytes;
  610. CURLcode result = CURLE_OK;
  611. struct SingleRequest *k = &data->req;
  612. size_t cb; /* Bytes currently read */
  613. char buffer[STRERROR_LEN];
  614. char *bufptr;
  615. bool eos;
  616. switch(event) {
  617. case TFTP_EVENT_ACK:
  618. case TFTP_EVENT_OACK:
  619. if(event == TFTP_EVENT_ACK) {
  620. /* Ack the packet */
  621. int rblock = getrpacketblock(&state->rpacket);
  622. if(rblock != state->block &&
  623. /* There is a bug in tftpd-hpa that causes it to send us an ack for
  624. * 65535 when the block number wraps to 0. So when we are expecting
  625. * 0, also accept 65535. See
  626. * https://www.syslinux.org/archives/2010-September/015612.html
  627. * */
  628. !(state->block == 0 && rblock == 65535)) {
  629. /* This is not the expected block. Log it and up the retry counter */
  630. infof(data, "Received ACK for block %d, expecting %d",
  631. rblock, state->block);
  632. state->retries++;
  633. /* Bail out if over the maximum */
  634. if(state->retries > state->retry_max) {
  635. failf(data, "tftp_tx: giving up waiting for block %d ack",
  636. state->block);
  637. result = CURLE_SEND_ERROR;
  638. }
  639. else {
  640. /* Re-send the data packet */
  641. sbytes = sendto(state->sockfd, (void *)state->spacket.data,
  642. 4 + (SEND_TYPE_ARG3)state->sbytes, SEND_4TH_ARG,
  643. (struct sockaddr *)&state->remote_addr,
  644. state->remote_addrlen);
  645. /* Check all sbytes were sent */
  646. if(sbytes < 0) {
  647. failf(data, "%s", Curl_strerror(SOCKERRNO,
  648. buffer, sizeof(buffer)));
  649. result = CURLE_SEND_ERROR;
  650. }
  651. }
  652. return result;
  653. }
  654. /* This is the expected packet. Reset the counters and send the next
  655. block */
  656. state->rx_time = time(NULL);
  657. state->block++;
  658. }
  659. else
  660. state->block = 1; /* first data block is 1 when using OACK */
  661. state->retries = 0;
  662. setpacketevent(&state->spacket, TFTP_EVENT_DATA);
  663. setpacketblock(&state->spacket, state->block);
  664. if(state->block > 1 && state->sbytes < state->blksize) {
  665. state->state = TFTP_STATE_FIN;
  666. return CURLE_OK;
  667. }
  668. /* TFTP considers data block size < 512 bytes as an end of session. So
  669. * in some cases we must wait for additional data to build full (512 bytes)
  670. * data block.
  671. * */
  672. state->sbytes = 0;
  673. bufptr = (char *)state->spacket.data + 4;
  674. do {
  675. result = Curl_client_read(data, bufptr, state->blksize - state->sbytes,
  676. &cb, &eos);
  677. if(result)
  678. return result;
  679. state->sbytes += (int)cb;
  680. bufptr += cb;
  681. } while(state->sbytes < state->blksize && cb);
  682. sbytes = sendto(state->sockfd, (void *) state->spacket.data,
  683. 4 + (SEND_TYPE_ARG3)state->sbytes, SEND_4TH_ARG,
  684. (struct sockaddr *)&state->remote_addr,
  685. state->remote_addrlen);
  686. /* Check all sbytes were sent */
  687. if(sbytes < 0) {
  688. failf(data, "%s", Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
  689. return CURLE_SEND_ERROR;
  690. }
  691. /* Update the progress meter */
  692. k->writebytecount += state->sbytes;
  693. Curl_pgrsSetUploadCounter(data, k->writebytecount);
  694. break;
  695. case TFTP_EVENT_TIMEOUT:
  696. /* Increment the retry counter and log the timeout */
  697. state->retries++;
  698. infof(data, "Timeout waiting for block %d ACK. "
  699. " Retries = %d", NEXT_BLOCKNUM(state->block), state->retries);
  700. /* Decide if we have had enough */
  701. if(state->retries > state->retry_max) {
  702. state->error = TFTP_ERR_TIMEOUT;
  703. state->state = TFTP_STATE_FIN;
  704. }
  705. else {
  706. /* Re-send the data packet */
  707. sbytes = sendto(state->sockfd, (void *)state->spacket.data,
  708. 4 + (SEND_TYPE_ARG3)state->sbytes, SEND_4TH_ARG,
  709. (struct sockaddr *)&state->remote_addr,
  710. state->remote_addrlen);
  711. /* Check all sbytes were sent */
  712. if(sbytes < 0) {
  713. failf(data, "%s", Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
  714. return CURLE_SEND_ERROR;
  715. }
  716. /* since this was a re-send, we remain at the still byte position */
  717. Curl_pgrsSetUploadCounter(data, k->writebytecount);
  718. }
  719. break;
  720. case TFTP_EVENT_ERROR:
  721. state->state = TFTP_STATE_FIN;
  722. setpacketevent(&state->spacket, TFTP_EVENT_ERROR);
  723. setpacketblock(&state->spacket, state->block);
  724. (void)sendto(state->sockfd, (void *)state->spacket.data, 4, SEND_4TH_ARG,
  725. (struct sockaddr *)&state->remote_addr,
  726. state->remote_addrlen);
  727. /* do not bother with the return code, but if the socket is still up we
  728. * should be a good TFTP client and let the server know we are done */
  729. state->state = TFTP_STATE_FIN;
  730. break;
  731. default:
  732. failf(data, "tftp_tx: internal error, event: %i", (int)(event));
  733. break;
  734. }
  735. return result;
  736. }
  737. /**********************************************************
  738. *
  739. * tftp_translate_code
  740. *
  741. * Translate internal error codes to CURL error codes
  742. *
  743. **********************************************************/
  744. static CURLcode tftp_translate_code(tftp_error_t error)
  745. {
  746. CURLcode result = CURLE_OK;
  747. if(error != TFTP_ERR_NONE) {
  748. switch(error) {
  749. case TFTP_ERR_NOTFOUND:
  750. result = CURLE_TFTP_NOTFOUND;
  751. break;
  752. case TFTP_ERR_PERM:
  753. result = CURLE_TFTP_PERM;
  754. break;
  755. case TFTP_ERR_DISKFULL:
  756. result = CURLE_REMOTE_DISK_FULL;
  757. break;
  758. case TFTP_ERR_UNDEF:
  759. case TFTP_ERR_ILLEGAL:
  760. result = CURLE_TFTP_ILLEGAL;
  761. break;
  762. case TFTP_ERR_UNKNOWNID:
  763. result = CURLE_TFTP_UNKNOWNID;
  764. break;
  765. case TFTP_ERR_EXISTS:
  766. result = CURLE_REMOTE_FILE_EXISTS;
  767. break;
  768. case TFTP_ERR_NOSUCHUSER:
  769. result = CURLE_TFTP_NOSUCHUSER;
  770. break;
  771. case TFTP_ERR_TIMEOUT:
  772. result = CURLE_OPERATION_TIMEDOUT;
  773. break;
  774. case TFTP_ERR_NORESPONSE:
  775. result = CURLE_COULDNT_CONNECT;
  776. break;
  777. default:
  778. result = CURLE_ABORTED_BY_CALLBACK;
  779. break;
  780. }
  781. }
  782. else
  783. result = CURLE_OK;
  784. return result;
  785. }
  786. /**********************************************************
  787. *
  788. * tftp_state_machine
  789. *
  790. * The tftp state machine event dispatcher
  791. *
  792. **********************************************************/
  793. static CURLcode tftp_state_machine(struct tftp_state_data *state,
  794. tftp_event_t event)
  795. {
  796. CURLcode result = CURLE_OK;
  797. struct Curl_easy *data = state->data;
  798. switch(state->state) {
  799. case TFTP_STATE_START:
  800. DEBUGF(infof(data, "TFTP_STATE_START"));
  801. result = tftp_send_first(state, event);
  802. break;
  803. case TFTP_STATE_RX:
  804. DEBUGF(infof(data, "TFTP_STATE_RX"));
  805. result = tftp_rx(state, event);
  806. break;
  807. case TFTP_STATE_TX:
  808. DEBUGF(infof(data, "TFTP_STATE_TX"));
  809. result = tftp_tx(state, event);
  810. break;
  811. case TFTP_STATE_FIN:
  812. infof(data, "%s", "TFTP finished");
  813. break;
  814. default:
  815. DEBUGF(infof(data, "STATE: %d", state->state));
  816. failf(data, "%s", "Internal state machine error");
  817. result = CURLE_TFTP_ILLEGAL;
  818. break;
  819. }
  820. return result;
  821. }
  822. /**********************************************************
  823. *
  824. * tftp_disconnect
  825. *
  826. * The disconnect callback
  827. *
  828. **********************************************************/
  829. static CURLcode tftp_disconnect(struct Curl_easy *data,
  830. struct connectdata *conn, bool dead_connection)
  831. {
  832. struct tftp_state_data *state = conn->proto.tftpc;
  833. (void) data;
  834. (void) dead_connection;
  835. /* done, free dynamically allocated pkt buffers */
  836. if(state) {
  837. Curl_safefree(state->rpacket.data);
  838. Curl_safefree(state->spacket.data);
  839. free(state);
  840. }
  841. return CURLE_OK;
  842. }
  843. /**********************************************************
  844. *
  845. * tftp_connect
  846. *
  847. * The connect callback
  848. *
  849. **********************************************************/
  850. static CURLcode tftp_connect(struct Curl_easy *data, bool *done)
  851. {
  852. struct tftp_state_data *state;
  853. int blksize;
  854. int need_blksize;
  855. struct connectdata *conn = data->conn;
  856. blksize = TFTP_BLKSIZE_DEFAULT;
  857. state = conn->proto.tftpc = calloc(1, sizeof(struct tftp_state_data));
  858. if(!state)
  859. return CURLE_OUT_OF_MEMORY;
  860. /* alloc pkt buffers based on specified blksize */
  861. if(data->set.tftp_blksize)
  862. /* range checked when set */
  863. blksize = (int)data->set.tftp_blksize;
  864. need_blksize = blksize;
  865. /* default size is the fallback when no OACK is received */
  866. if(need_blksize < TFTP_BLKSIZE_DEFAULT)
  867. need_blksize = TFTP_BLKSIZE_DEFAULT;
  868. if(!state->rpacket.data) {
  869. state->rpacket.data = calloc(1, need_blksize + 2 + 2);
  870. if(!state->rpacket.data)
  871. return CURLE_OUT_OF_MEMORY;
  872. }
  873. if(!state->spacket.data) {
  874. state->spacket.data = calloc(1, need_blksize + 2 + 2);
  875. if(!state->spacket.data)
  876. return CURLE_OUT_OF_MEMORY;
  877. }
  878. /* we do not keep TFTP connections up basically because there is none or
  879. * little gain for UDP */
  880. connclose(conn, "TFTP");
  881. state->data = data;
  882. state->sockfd = conn->sock[FIRSTSOCKET];
  883. state->state = TFTP_STATE_START;
  884. state->error = TFTP_ERR_NONE;
  885. state->blksize = TFTP_BLKSIZE_DEFAULT; /* Unless updated by OACK response */
  886. state->requested_blksize = blksize;
  887. ((struct sockaddr *)&state->local_addr)->sa_family =
  888. (CURL_SA_FAMILY_T)(conn->remote_addr->family);
  889. tftp_set_timeouts(state);
  890. if(!conn->bits.bound) {
  891. /* If not already bound, bind to any interface, random UDP port. If it is
  892. * reused or a custom local port was desired, this has already been done!
  893. *
  894. * We once used the size of the local_addr struct as the third argument
  895. * for bind() to better work with IPv6 or whatever size the struct could
  896. * have, but we learned that at least Tru64, AIX and IRIX *requires* the
  897. * size of that argument to match the exact size of a 'sockaddr_in' struct
  898. * when running IPv4-only.
  899. *
  900. * Therefore we use the size from the address we connected to, which we
  901. * assume uses the same IP version and thus hopefully this works for both
  902. * IPv4 and IPv6...
  903. */
  904. int rc = bind(state->sockfd, (struct sockaddr *)&state->local_addr,
  905. (curl_socklen_t)conn->remote_addr->addrlen);
  906. if(rc) {
  907. char buffer[STRERROR_LEN];
  908. failf(data, "bind() failed; %s",
  909. Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
  910. return CURLE_COULDNT_CONNECT;
  911. }
  912. conn->bits.bound = TRUE;
  913. }
  914. Curl_pgrsStartNow(data);
  915. *done = TRUE;
  916. return CURLE_OK;
  917. }
  918. /**********************************************************
  919. *
  920. * tftp_done
  921. *
  922. * The done callback
  923. *
  924. **********************************************************/
  925. static CURLcode tftp_done(struct Curl_easy *data, CURLcode status,
  926. bool premature)
  927. {
  928. CURLcode result = CURLE_OK;
  929. struct connectdata *conn = data->conn;
  930. struct tftp_state_data *state = conn->proto.tftpc;
  931. (void)status; /* unused */
  932. (void)premature; /* not used */
  933. if(Curl_pgrsDone(data))
  934. return CURLE_ABORTED_BY_CALLBACK;
  935. /* If we have encountered an error */
  936. if(state)
  937. result = tftp_translate_code(state->error);
  938. return result;
  939. }
  940. /**********************************************************
  941. *
  942. * tftp_getsock
  943. *
  944. * The getsock callback
  945. *
  946. **********************************************************/
  947. static int tftp_getsock(struct Curl_easy *data,
  948. struct connectdata *conn, curl_socket_t *socks)
  949. {
  950. (void)data;
  951. socks[0] = conn->sock[FIRSTSOCKET];
  952. return GETSOCK_READSOCK(0);
  953. }
  954. /**********************************************************
  955. *
  956. * tftp_receive_packet
  957. *
  958. * Called once select fires and data is ready on the socket
  959. *
  960. **********************************************************/
  961. static CURLcode tftp_receive_packet(struct Curl_easy *data)
  962. {
  963. curl_socklen_t fromlen;
  964. CURLcode result = CURLE_OK;
  965. struct connectdata *conn = data->conn;
  966. struct tftp_state_data *state = conn->proto.tftpc;
  967. /* Receive the packet */
  968. fromlen = sizeof(state->remote_addr);
  969. state->rbytes = (int)recvfrom(state->sockfd,
  970. (void *)state->rpacket.data,
  971. (RECV_TYPE_ARG3)state->blksize + 4,
  972. 0,
  973. (struct sockaddr *)&state->remote_addr,
  974. &fromlen);
  975. state->remote_addrlen = fromlen;
  976. /* Sanity check packet length */
  977. if(state->rbytes < 4) {
  978. failf(data, "Received too short packet");
  979. /* Not a timeout, but how best to handle it? */
  980. state->event = TFTP_EVENT_TIMEOUT;
  981. }
  982. else {
  983. /* The event is given by the TFTP packet time */
  984. unsigned short event = getrpacketevent(&state->rpacket);
  985. state->event = (tftp_event_t)event;
  986. switch(state->event) {
  987. case TFTP_EVENT_DATA:
  988. /* Do not pass to the client empty or retransmitted packets */
  989. if(state->rbytes > 4 &&
  990. (NEXT_BLOCKNUM(state->block) == getrpacketblock(&state->rpacket))) {
  991. result = Curl_client_write(data, CLIENTWRITE_BODY,
  992. (char *)state->rpacket.data + 4,
  993. state->rbytes-4);
  994. if(result) {
  995. tftp_state_machine(state, TFTP_EVENT_ERROR);
  996. return result;
  997. }
  998. }
  999. break;
  1000. case TFTP_EVENT_ERROR:
  1001. {
  1002. unsigned short error = getrpacketblock(&state->rpacket);
  1003. char *str = (char *)state->rpacket.data + 4;
  1004. size_t strn = state->rbytes - 4;
  1005. state->error = (tftp_error_t)error;
  1006. if(tftp_strnlen(str, strn) < strn)
  1007. infof(data, "TFTP error: %s", str);
  1008. break;
  1009. }
  1010. case TFTP_EVENT_ACK:
  1011. break;
  1012. case TFTP_EVENT_OACK:
  1013. result = tftp_parse_option_ack(state,
  1014. (const char *)state->rpacket.data + 2,
  1015. state->rbytes-2);
  1016. if(result)
  1017. return result;
  1018. break;
  1019. case TFTP_EVENT_RRQ:
  1020. case TFTP_EVENT_WRQ:
  1021. default:
  1022. failf(data, "%s", "Internal error: Unexpected packet");
  1023. break;
  1024. }
  1025. /* Update the progress meter */
  1026. if(Curl_pgrsUpdate(data)) {
  1027. tftp_state_machine(state, TFTP_EVENT_ERROR);
  1028. return CURLE_ABORTED_BY_CALLBACK;
  1029. }
  1030. }
  1031. return result;
  1032. }
  1033. /**********************************************************
  1034. *
  1035. * tftp_state_timeout
  1036. *
  1037. * Check if timeouts have been reached
  1038. *
  1039. **********************************************************/
  1040. static timediff_t tftp_state_timeout(struct Curl_easy *data,
  1041. tftp_event_t *event)
  1042. {
  1043. time_t current;
  1044. struct connectdata *conn = data->conn;
  1045. struct tftp_state_data *state = conn->proto.tftpc;
  1046. timediff_t timeout_ms;
  1047. if(event)
  1048. *event = TFTP_EVENT_NONE;
  1049. timeout_ms = Curl_timeleft(state->data, NULL,
  1050. (state->state == TFTP_STATE_START));
  1051. if(timeout_ms < 0) {
  1052. state->error = TFTP_ERR_TIMEOUT;
  1053. state->state = TFTP_STATE_FIN;
  1054. return 0;
  1055. }
  1056. current = time(NULL);
  1057. if(current > state->rx_time + state->retry_time) {
  1058. if(event)
  1059. *event = TFTP_EVENT_TIMEOUT;
  1060. state->rx_time = time(NULL); /* update even though we received nothing */
  1061. }
  1062. return timeout_ms;
  1063. }
  1064. /**********************************************************
  1065. *
  1066. * tftp_multi_statemach
  1067. *
  1068. * Handle single RX socket event and return
  1069. *
  1070. **********************************************************/
  1071. static CURLcode tftp_multi_statemach(struct Curl_easy *data, bool *done)
  1072. {
  1073. tftp_event_t event;
  1074. CURLcode result = CURLE_OK;
  1075. struct connectdata *conn = data->conn;
  1076. struct tftp_state_data *state = conn->proto.tftpc;
  1077. timediff_t timeout_ms = tftp_state_timeout(data, &event);
  1078. *done = FALSE;
  1079. if(timeout_ms < 0) {
  1080. failf(data, "TFTP response timeout");
  1081. return CURLE_OPERATION_TIMEDOUT;
  1082. }
  1083. if(event != TFTP_EVENT_NONE) {
  1084. result = tftp_state_machine(state, event);
  1085. if(result)
  1086. return result;
  1087. *done = (state->state == TFTP_STATE_FIN);
  1088. if(*done)
  1089. /* Tell curl we are done */
  1090. Curl_xfer_setup_nop(data);
  1091. }
  1092. else {
  1093. /* no timeouts to handle, check our socket */
  1094. int rc = SOCKET_READABLE(state->sockfd, 0);
  1095. if(rc == -1) {
  1096. /* bail out */
  1097. int error = SOCKERRNO;
  1098. char buffer[STRERROR_LEN];
  1099. failf(data, "%s", Curl_strerror(error, buffer, sizeof(buffer)));
  1100. state->event = TFTP_EVENT_ERROR;
  1101. }
  1102. else if(rc) {
  1103. result = tftp_receive_packet(data);
  1104. if(result)
  1105. return result;
  1106. result = tftp_state_machine(state, state->event);
  1107. if(result)
  1108. return result;
  1109. *done = (state->state == TFTP_STATE_FIN);
  1110. if(*done)
  1111. /* Tell curl we are done */
  1112. Curl_xfer_setup_nop(data);
  1113. }
  1114. /* if rc == 0, then select() timed out */
  1115. }
  1116. return result;
  1117. }
  1118. /**********************************************************
  1119. *
  1120. * tftp_doing
  1121. *
  1122. * Called from multi.c while DOing
  1123. *
  1124. **********************************************************/
  1125. static CURLcode tftp_doing(struct Curl_easy *data, bool *dophase_done)
  1126. {
  1127. CURLcode result;
  1128. result = tftp_multi_statemach(data, dophase_done);
  1129. if(*dophase_done) {
  1130. DEBUGF(infof(data, "DO phase is complete"));
  1131. }
  1132. else if(!result) {
  1133. /* The multi code does not have this logic for the DOING state so we
  1134. provide it for TFTP since it may do the entire transfer in this
  1135. state. */
  1136. if(Curl_pgrsUpdate(data))
  1137. result = CURLE_ABORTED_BY_CALLBACK;
  1138. else
  1139. result = Curl_speedcheck(data, Curl_now());
  1140. }
  1141. return result;
  1142. }
  1143. /**********************************************************
  1144. *
  1145. * tftp_perform
  1146. *
  1147. * Entry point for transfer from tftp_do, starts state mach
  1148. *
  1149. **********************************************************/
  1150. static CURLcode tftp_perform(struct Curl_easy *data, bool *dophase_done)
  1151. {
  1152. CURLcode result = CURLE_OK;
  1153. struct connectdata *conn = data->conn;
  1154. struct tftp_state_data *state = conn->proto.tftpc;
  1155. *dophase_done = FALSE;
  1156. result = tftp_state_machine(state, TFTP_EVENT_INIT);
  1157. if((state->state == TFTP_STATE_FIN) || result)
  1158. return result;
  1159. tftp_multi_statemach(data, dophase_done);
  1160. if(*dophase_done)
  1161. DEBUGF(infof(data, "DO phase is complete"));
  1162. return result;
  1163. }
  1164. /**********************************************************
  1165. *
  1166. * tftp_do
  1167. *
  1168. * The do callback
  1169. *
  1170. * This callback initiates the TFTP transfer
  1171. *
  1172. **********************************************************/
  1173. static CURLcode tftp_do(struct Curl_easy *data, bool *done)
  1174. {
  1175. struct tftp_state_data *state;
  1176. CURLcode result;
  1177. struct connectdata *conn = data->conn;
  1178. *done = FALSE;
  1179. if(!conn->proto.tftpc) {
  1180. result = tftp_connect(data, done);
  1181. if(result)
  1182. return result;
  1183. }
  1184. state = conn->proto.tftpc;
  1185. if(!state)
  1186. return CURLE_TFTP_ILLEGAL;
  1187. result = tftp_perform(data, done);
  1188. /* If tftp_perform() returned an error, use that for return code. If it
  1189. was OK, see if tftp_translate_code() has an error. */
  1190. if(!result)
  1191. /* If we have encountered an internal tftp error, translate it. */
  1192. result = tftp_translate_code(state->error);
  1193. return result;
  1194. }
  1195. static CURLcode tftp_setup_connection(struct Curl_easy *data,
  1196. struct connectdata *conn)
  1197. {
  1198. char *type;
  1199. conn->transport = TRNSPRT_UDP;
  1200. /* TFTP URLs support an extension like ";mode=<typecode>" that
  1201. * we will try to get now! */
  1202. type = strstr(data->state.up.path, ";mode=");
  1203. if(!type)
  1204. type = strstr(conn->host.rawalloc, ";mode=");
  1205. if(type) {
  1206. char command;
  1207. *type = 0; /* it was in the middle of the hostname */
  1208. command = Curl_raw_toupper(type[6]);
  1209. switch(command) {
  1210. case 'A': /* ASCII mode */
  1211. case 'N': /* NETASCII mode */
  1212. data->state.prefer_ascii = TRUE;
  1213. break;
  1214. case 'O': /* octet mode */
  1215. case 'I': /* binary mode */
  1216. default:
  1217. /* switch off ASCII */
  1218. data->state.prefer_ascii = FALSE;
  1219. break;
  1220. }
  1221. }
  1222. return CURLE_OK;
  1223. }
  1224. #endif