tftp.c 41 KB

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