tftp.c 40 KB

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