tftp.c 40 KB

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