tftp.c 40 KB

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