tftp.c 40 KB

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