tftp.c 40 KB

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