tftp.c 41 KB

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