2
0

telnet.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, 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 https://curl.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #ifndef CURL_DISABLE_TELNET
  26. #ifdef HAVE_NETINET_IN_H
  27. #include <netinet/in.h>
  28. #endif
  29. #ifdef HAVE_NETDB_H
  30. #include <netdb.h>
  31. #endif
  32. #ifdef HAVE_ARPA_INET_H
  33. #include <arpa/inet.h>
  34. #endif
  35. #ifdef HAVE_NET_IF_H
  36. #include <net/if.h>
  37. #endif
  38. #ifdef HAVE_SYS_IOCTL_H
  39. #include <sys/ioctl.h>
  40. #endif
  41. #ifdef HAVE_SYS_PARAM_H
  42. #include <sys/param.h>
  43. #endif
  44. #include "urldata.h"
  45. #include <curl/curl.h>
  46. #include "transfer.h"
  47. #include "sendf.h"
  48. #include "telnet.h"
  49. #include "connect.h"
  50. #include "progress.h"
  51. #include "system_win32.h"
  52. #include "arpa_telnet.h"
  53. #include "select.h"
  54. #include "strcase.h"
  55. #include "warnless.h"
  56. /* The last 3 #include files should be in this order */
  57. #include "curl_printf.h"
  58. #include "curl_memory.h"
  59. #include "memdebug.h"
  60. #define SUBBUFSIZE 512
  61. #define CURL_SB_CLEAR(x) x->subpointer = x->subbuffer
  62. #define CURL_SB_TERM(x) \
  63. do { \
  64. x->subend = x->subpointer; \
  65. CURL_SB_CLEAR(x); \
  66. } while(0)
  67. #define CURL_SB_ACCUM(x,c) \
  68. do { \
  69. if(x->subpointer < (x->subbuffer + sizeof(x->subbuffer))) \
  70. *x->subpointer++ = (c); \
  71. } while(0)
  72. #define CURL_SB_GET(x) ((*x->subpointer++)&0xff)
  73. #define CURL_SB_LEN(x) (x->subend - x->subpointer)
  74. /* For posterity:
  75. #define CURL_SB_PEEK(x) ((*x->subpointer)&0xff)
  76. #define CURL_SB_EOF(x) (x->subpointer >= x->subend) */
  77. #ifdef CURL_DISABLE_VERBOSE_STRINGS
  78. #define printoption(a,b,c,d) Curl_nop_stmt
  79. #endif
  80. static
  81. CURLcode telrcv(struct Curl_easy *data,
  82. const unsigned char *inbuf, /* Data received from socket */
  83. ssize_t count); /* Number of bytes received */
  84. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  85. static void printoption(struct Curl_easy *data,
  86. const char *direction,
  87. int cmd, int option);
  88. #endif
  89. static void negotiate(struct Curl_easy *data);
  90. static void send_negotiation(struct Curl_easy *data, int cmd, int option);
  91. static void set_local_option(struct Curl_easy *data,
  92. int option, int newstate);
  93. static void set_remote_option(struct Curl_easy *data,
  94. int option, int newstate);
  95. static void printsub(struct Curl_easy *data,
  96. int direction, unsigned char *pointer,
  97. size_t length);
  98. static void suboption(struct Curl_easy *data);
  99. static void sendsuboption(struct Curl_easy *data, int option);
  100. static CURLcode telnet_do(struct Curl_easy *data, bool *done);
  101. static CURLcode telnet_done(struct Curl_easy *data,
  102. CURLcode, bool premature);
  103. static CURLcode send_telnet_data(struct Curl_easy *data,
  104. char *buffer, ssize_t nread);
  105. /* For negotiation compliant to RFC 1143 */
  106. #define CURL_NO 0
  107. #define CURL_YES 1
  108. #define CURL_WANTYES 2
  109. #define CURL_WANTNO 3
  110. #define CURL_EMPTY 0
  111. #define CURL_OPPOSITE 1
  112. /*
  113. * Telnet receiver states for fsm
  114. */
  115. typedef enum
  116. {
  117. CURL_TS_DATA = 0,
  118. CURL_TS_IAC,
  119. CURL_TS_WILL,
  120. CURL_TS_WONT,
  121. CURL_TS_DO,
  122. CURL_TS_DONT,
  123. CURL_TS_CR,
  124. CURL_TS_SB, /* sub-option collection */
  125. CURL_TS_SE /* looking for sub-option end */
  126. } TelnetReceive;
  127. struct TELNET {
  128. int please_negotiate;
  129. int already_negotiated;
  130. int us[256];
  131. int usq[256];
  132. int us_preferred[256];
  133. int him[256];
  134. int himq[256];
  135. int him_preferred[256];
  136. int subnegotiation[256];
  137. char subopt_ttype[32]; /* Set with suboption TTYPE */
  138. char subopt_xdisploc[128]; /* Set with suboption XDISPLOC */
  139. unsigned short subopt_wsx; /* Set with suboption NAWS */
  140. unsigned short subopt_wsy; /* Set with suboption NAWS */
  141. TelnetReceive telrcv_state;
  142. struct curl_slist *telnet_vars; /* Environment variables */
  143. /* suboptions */
  144. unsigned char subbuffer[SUBBUFSIZE];
  145. unsigned char *subpointer, *subend; /* buffer for sub-options */
  146. };
  147. /*
  148. * TELNET protocol handler.
  149. */
  150. const struct Curl_handler Curl_handler_telnet = {
  151. "TELNET", /* scheme */
  152. ZERO_NULL, /* setup_connection */
  153. telnet_do, /* do_it */
  154. telnet_done, /* done */
  155. ZERO_NULL, /* do_more */
  156. ZERO_NULL, /* connect_it */
  157. ZERO_NULL, /* connecting */
  158. ZERO_NULL, /* doing */
  159. ZERO_NULL, /* proto_getsock */
  160. ZERO_NULL, /* doing_getsock */
  161. ZERO_NULL, /* domore_getsock */
  162. ZERO_NULL, /* perform_getsock */
  163. ZERO_NULL, /* disconnect */
  164. ZERO_NULL, /* readwrite */
  165. ZERO_NULL, /* connection_check */
  166. ZERO_NULL, /* attach connection */
  167. PORT_TELNET, /* defport */
  168. CURLPROTO_TELNET, /* protocol */
  169. CURLPROTO_TELNET, /* family */
  170. PROTOPT_NONE | PROTOPT_NOURLQUERY /* flags */
  171. };
  172. static
  173. CURLcode init_telnet(struct Curl_easy *data)
  174. {
  175. struct TELNET *tn;
  176. tn = calloc(1, sizeof(struct TELNET));
  177. if(!tn)
  178. return CURLE_OUT_OF_MEMORY;
  179. data->req.p.telnet = tn; /* make us known */
  180. tn->telrcv_state = CURL_TS_DATA;
  181. /* Init suboptions */
  182. CURL_SB_CLEAR(tn);
  183. /* Set the options we want by default */
  184. tn->us_preferred[CURL_TELOPT_SGA] = CURL_YES;
  185. tn->him_preferred[CURL_TELOPT_SGA] = CURL_YES;
  186. /* To be compliant with previous releases of libcurl
  187. we enable this option by default. This behavior
  188. can be changed thanks to the "BINARY" option in
  189. CURLOPT_TELNETOPTIONS
  190. */
  191. tn->us_preferred[CURL_TELOPT_BINARY] = CURL_YES;
  192. tn->him_preferred[CURL_TELOPT_BINARY] = CURL_YES;
  193. /* We must allow the server to echo what we sent
  194. but it is not necessary to request the server
  195. to do so (it might forces the server to close
  196. the connection). Hence, we ignore ECHO in the
  197. negotiate function
  198. */
  199. tn->him_preferred[CURL_TELOPT_ECHO] = CURL_YES;
  200. /* Set the subnegotiation fields to send information
  201. just after negotiation passed (do/will)
  202. Default values are (0,0) initialized by calloc.
  203. According to the RFC1013 it is valid:
  204. A value equal to zero is acceptable for the width (or height),
  205. and means that no character width (or height) is being sent.
  206. In this case, the width (or height) that will be assumed by the
  207. Telnet server is operating system specific (it will probably be
  208. based upon the terminal type information that may have been sent
  209. using the TERMINAL TYPE Telnet option). */
  210. tn->subnegotiation[CURL_TELOPT_NAWS] = CURL_YES;
  211. return CURLE_OK;
  212. }
  213. static void negotiate(struct Curl_easy *data)
  214. {
  215. int i;
  216. struct TELNET *tn = data->req.p.telnet;
  217. for(i = 0; i < CURL_NTELOPTS; i++) {
  218. if(i == CURL_TELOPT_ECHO)
  219. continue;
  220. if(tn->us_preferred[i] == CURL_YES)
  221. set_local_option(data, i, CURL_YES);
  222. if(tn->him_preferred[i] == CURL_YES)
  223. set_remote_option(data, i, CURL_YES);
  224. }
  225. }
  226. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  227. static void printoption(struct Curl_easy *data,
  228. const char *direction, int cmd, int option)
  229. {
  230. if(data->set.verbose) {
  231. if(cmd == CURL_IAC) {
  232. if(CURL_TELCMD_OK(option))
  233. infof(data, "%s IAC %s", direction, CURL_TELCMD(option));
  234. else
  235. infof(data, "%s IAC %d", direction, option);
  236. }
  237. else {
  238. const char *fmt = (cmd == CURL_WILL) ? "WILL" :
  239. (cmd == CURL_WONT) ? "WONT" :
  240. (cmd == CURL_DO) ? "DO" :
  241. (cmd == CURL_DONT) ? "DONT" : 0;
  242. if(fmt) {
  243. const char *opt;
  244. if(CURL_TELOPT_OK(option))
  245. opt = CURL_TELOPT(option);
  246. else if(option == CURL_TELOPT_EXOPL)
  247. opt = "EXOPL";
  248. else
  249. opt = NULL;
  250. if(opt)
  251. infof(data, "%s %s %s", direction, fmt, opt);
  252. else
  253. infof(data, "%s %s %d", direction, fmt, option);
  254. }
  255. else
  256. infof(data, "%s %d %d", direction, cmd, option);
  257. }
  258. }
  259. }
  260. #endif
  261. static void send_negotiation(struct Curl_easy *data, int cmd, int option)
  262. {
  263. unsigned char buf[3];
  264. ssize_t bytes_written;
  265. struct connectdata *conn = data->conn;
  266. buf[0] = CURL_IAC;
  267. buf[1] = (unsigned char)cmd;
  268. buf[2] = (unsigned char)option;
  269. bytes_written = swrite(conn->sock[FIRSTSOCKET], buf, 3);
  270. if(bytes_written < 0) {
  271. int err = SOCKERRNO;
  272. failf(data,"Sending data failed (%d)",err);
  273. }
  274. printoption(data, "SENT", cmd, option);
  275. }
  276. static
  277. void set_remote_option(struct Curl_easy *data, int option, int newstate)
  278. {
  279. struct TELNET *tn = data->req.p.telnet;
  280. if(newstate == CURL_YES) {
  281. switch(tn->him[option]) {
  282. case CURL_NO:
  283. tn->him[option] = CURL_WANTYES;
  284. send_negotiation(data, CURL_DO, option);
  285. break;
  286. case CURL_YES:
  287. /* Already enabled */
  288. break;
  289. case CURL_WANTNO:
  290. switch(tn->himq[option]) {
  291. case CURL_EMPTY:
  292. /* Already negotiating for CURL_YES, queue the request */
  293. tn->himq[option] = CURL_OPPOSITE;
  294. break;
  295. case CURL_OPPOSITE:
  296. /* Error: already queued an enable request */
  297. break;
  298. }
  299. break;
  300. case CURL_WANTYES:
  301. switch(tn->himq[option]) {
  302. case CURL_EMPTY:
  303. /* Error: already negotiating for enable */
  304. break;
  305. case CURL_OPPOSITE:
  306. tn->himq[option] = CURL_EMPTY;
  307. break;
  308. }
  309. break;
  310. }
  311. }
  312. else { /* NO */
  313. switch(tn->him[option]) {
  314. case CURL_NO:
  315. /* Already disabled */
  316. break;
  317. case CURL_YES:
  318. tn->him[option] = CURL_WANTNO;
  319. send_negotiation(data, CURL_DONT, option);
  320. break;
  321. case CURL_WANTNO:
  322. switch(tn->himq[option]) {
  323. case CURL_EMPTY:
  324. /* Already negotiating for NO */
  325. break;
  326. case CURL_OPPOSITE:
  327. tn->himq[option] = CURL_EMPTY;
  328. break;
  329. }
  330. break;
  331. case CURL_WANTYES:
  332. switch(tn->himq[option]) {
  333. case CURL_EMPTY:
  334. tn->himq[option] = CURL_OPPOSITE;
  335. break;
  336. case CURL_OPPOSITE:
  337. break;
  338. }
  339. break;
  340. }
  341. }
  342. }
  343. static
  344. void rec_will(struct Curl_easy *data, int option)
  345. {
  346. struct TELNET *tn = data->req.p.telnet;
  347. switch(tn->him[option]) {
  348. case CURL_NO:
  349. if(tn->him_preferred[option] == CURL_YES) {
  350. tn->him[option] = CURL_YES;
  351. send_negotiation(data, CURL_DO, option);
  352. }
  353. else
  354. send_negotiation(data, CURL_DONT, option);
  355. break;
  356. case CURL_YES:
  357. /* Already enabled */
  358. break;
  359. case CURL_WANTNO:
  360. switch(tn->himq[option]) {
  361. case CURL_EMPTY:
  362. /* Error: DONT answered by WILL */
  363. tn->him[option] = CURL_NO;
  364. break;
  365. case CURL_OPPOSITE:
  366. /* Error: DONT answered by WILL */
  367. tn->him[option] = CURL_YES;
  368. tn->himq[option] = CURL_EMPTY;
  369. break;
  370. }
  371. break;
  372. case CURL_WANTYES:
  373. switch(tn->himq[option]) {
  374. case CURL_EMPTY:
  375. tn->him[option] = CURL_YES;
  376. break;
  377. case CURL_OPPOSITE:
  378. tn->him[option] = CURL_WANTNO;
  379. tn->himq[option] = CURL_EMPTY;
  380. send_negotiation(data, CURL_DONT, option);
  381. break;
  382. }
  383. break;
  384. }
  385. }
  386. static
  387. void rec_wont(struct Curl_easy *data, int option)
  388. {
  389. struct TELNET *tn = data->req.p.telnet;
  390. switch(tn->him[option]) {
  391. case CURL_NO:
  392. /* Already disabled */
  393. break;
  394. case CURL_YES:
  395. tn->him[option] = CURL_NO;
  396. send_negotiation(data, CURL_DONT, option);
  397. break;
  398. case CURL_WANTNO:
  399. switch(tn->himq[option]) {
  400. case CURL_EMPTY:
  401. tn->him[option] = CURL_NO;
  402. break;
  403. case CURL_OPPOSITE:
  404. tn->him[option] = CURL_WANTYES;
  405. tn->himq[option] = CURL_EMPTY;
  406. send_negotiation(data, CURL_DO, option);
  407. break;
  408. }
  409. break;
  410. case CURL_WANTYES:
  411. switch(tn->himq[option]) {
  412. case CURL_EMPTY:
  413. tn->him[option] = CURL_NO;
  414. break;
  415. case CURL_OPPOSITE:
  416. tn->him[option] = CURL_NO;
  417. tn->himq[option] = CURL_EMPTY;
  418. break;
  419. }
  420. break;
  421. }
  422. }
  423. static void
  424. set_local_option(struct Curl_easy *data, int option, int newstate)
  425. {
  426. struct TELNET *tn = data->req.p.telnet;
  427. if(newstate == CURL_YES) {
  428. switch(tn->us[option]) {
  429. case CURL_NO:
  430. tn->us[option] = CURL_WANTYES;
  431. send_negotiation(data, CURL_WILL, option);
  432. break;
  433. case CURL_YES:
  434. /* Already enabled */
  435. break;
  436. case CURL_WANTNO:
  437. switch(tn->usq[option]) {
  438. case CURL_EMPTY:
  439. /* Already negotiating for CURL_YES, queue the request */
  440. tn->usq[option] = CURL_OPPOSITE;
  441. break;
  442. case CURL_OPPOSITE:
  443. /* Error: already queued an enable request */
  444. break;
  445. }
  446. break;
  447. case CURL_WANTYES:
  448. switch(tn->usq[option]) {
  449. case CURL_EMPTY:
  450. /* Error: already negotiating for enable */
  451. break;
  452. case CURL_OPPOSITE:
  453. tn->usq[option] = CURL_EMPTY;
  454. break;
  455. }
  456. break;
  457. }
  458. }
  459. else { /* NO */
  460. switch(tn->us[option]) {
  461. case CURL_NO:
  462. /* Already disabled */
  463. break;
  464. case CURL_YES:
  465. tn->us[option] = CURL_WANTNO;
  466. send_negotiation(data, CURL_WONT, option);
  467. break;
  468. case CURL_WANTNO:
  469. switch(tn->usq[option]) {
  470. case CURL_EMPTY:
  471. /* Already negotiating for NO */
  472. break;
  473. case CURL_OPPOSITE:
  474. tn->usq[option] = CURL_EMPTY;
  475. break;
  476. }
  477. break;
  478. case CURL_WANTYES:
  479. switch(tn->usq[option]) {
  480. case CURL_EMPTY:
  481. tn->usq[option] = CURL_OPPOSITE;
  482. break;
  483. case CURL_OPPOSITE:
  484. break;
  485. }
  486. break;
  487. }
  488. }
  489. }
  490. static
  491. void rec_do(struct Curl_easy *data, int option)
  492. {
  493. struct TELNET *tn = data->req.p.telnet;
  494. switch(tn->us[option]) {
  495. case CURL_NO:
  496. if(tn->us_preferred[option] == CURL_YES) {
  497. tn->us[option] = CURL_YES;
  498. send_negotiation(data, CURL_WILL, option);
  499. if(tn->subnegotiation[option] == CURL_YES)
  500. /* transmission of data option */
  501. sendsuboption(data, option);
  502. }
  503. else if(tn->subnegotiation[option] == CURL_YES) {
  504. /* send information to achieve this option */
  505. tn->us[option] = CURL_YES;
  506. send_negotiation(data, CURL_WILL, option);
  507. sendsuboption(data, option);
  508. }
  509. else
  510. send_negotiation(data, CURL_WONT, option);
  511. break;
  512. case CURL_YES:
  513. /* Already enabled */
  514. break;
  515. case CURL_WANTNO:
  516. switch(tn->usq[option]) {
  517. case CURL_EMPTY:
  518. /* Error: DONT answered by WILL */
  519. tn->us[option] = CURL_NO;
  520. break;
  521. case CURL_OPPOSITE:
  522. /* Error: DONT answered by WILL */
  523. tn->us[option] = CURL_YES;
  524. tn->usq[option] = CURL_EMPTY;
  525. break;
  526. }
  527. break;
  528. case CURL_WANTYES:
  529. switch(tn->usq[option]) {
  530. case CURL_EMPTY:
  531. tn->us[option] = CURL_YES;
  532. if(tn->subnegotiation[option] == CURL_YES) {
  533. /* transmission of data option */
  534. sendsuboption(data, option);
  535. }
  536. break;
  537. case CURL_OPPOSITE:
  538. tn->us[option] = CURL_WANTNO;
  539. tn->himq[option] = CURL_EMPTY;
  540. send_negotiation(data, CURL_WONT, option);
  541. break;
  542. }
  543. break;
  544. }
  545. }
  546. static
  547. void rec_dont(struct Curl_easy *data, int option)
  548. {
  549. struct TELNET *tn = data->req.p.telnet;
  550. switch(tn->us[option]) {
  551. case CURL_NO:
  552. /* Already disabled */
  553. break;
  554. case CURL_YES:
  555. tn->us[option] = CURL_NO;
  556. send_negotiation(data, CURL_WONT, option);
  557. break;
  558. case CURL_WANTNO:
  559. switch(tn->usq[option]) {
  560. case CURL_EMPTY:
  561. tn->us[option] = CURL_NO;
  562. break;
  563. case CURL_OPPOSITE:
  564. tn->us[option] = CURL_WANTYES;
  565. tn->usq[option] = CURL_EMPTY;
  566. send_negotiation(data, CURL_WILL, option);
  567. break;
  568. }
  569. break;
  570. case CURL_WANTYES:
  571. switch(tn->usq[option]) {
  572. case CURL_EMPTY:
  573. tn->us[option] = CURL_NO;
  574. break;
  575. case CURL_OPPOSITE:
  576. tn->us[option] = CURL_NO;
  577. tn->usq[option] = CURL_EMPTY;
  578. break;
  579. }
  580. break;
  581. }
  582. }
  583. static void printsub(struct Curl_easy *data,
  584. int direction, /* '<' or '>' */
  585. unsigned char *pointer, /* where suboption data is */
  586. size_t length) /* length of suboption data */
  587. {
  588. if(data->set.verbose) {
  589. unsigned int i = 0;
  590. if(direction) {
  591. infof(data, "%s IAC SB ", (direction == '<')? "RCVD":"SENT");
  592. if(length >= 3) {
  593. int j;
  594. i = pointer[length-2];
  595. j = pointer[length-1];
  596. if(i != CURL_IAC || j != CURL_SE) {
  597. infof(data, "(terminated by ");
  598. if(CURL_TELOPT_OK(i))
  599. infof(data, "%s ", CURL_TELOPT(i));
  600. else if(CURL_TELCMD_OK(i))
  601. infof(data, "%s ", CURL_TELCMD(i));
  602. else
  603. infof(data, "%u ", i);
  604. if(CURL_TELOPT_OK(j))
  605. infof(data, "%s", CURL_TELOPT(j));
  606. else if(CURL_TELCMD_OK(j))
  607. infof(data, "%s", CURL_TELCMD(j));
  608. else
  609. infof(data, "%d", j);
  610. infof(data, ", not IAC SE) ");
  611. }
  612. }
  613. length -= 2;
  614. }
  615. if(length < 1) {
  616. infof(data, "(Empty suboption?)");
  617. return;
  618. }
  619. if(CURL_TELOPT_OK(pointer[0])) {
  620. switch(pointer[0]) {
  621. case CURL_TELOPT_TTYPE:
  622. case CURL_TELOPT_XDISPLOC:
  623. case CURL_TELOPT_NEW_ENVIRON:
  624. case CURL_TELOPT_NAWS:
  625. infof(data, "%s", CURL_TELOPT(pointer[0]));
  626. break;
  627. default:
  628. infof(data, "%s (unsupported)", CURL_TELOPT(pointer[0]));
  629. break;
  630. }
  631. }
  632. else
  633. infof(data, "%d (unknown)", pointer[i]);
  634. switch(pointer[0]) {
  635. case CURL_TELOPT_NAWS:
  636. if(length > 4)
  637. infof(data, "Width: %d ; Height: %d", (pointer[1]<<8) | pointer[2],
  638. (pointer[3]<<8) | pointer[4]);
  639. break;
  640. default:
  641. switch(pointer[1]) {
  642. case CURL_TELQUAL_IS:
  643. infof(data, " IS");
  644. break;
  645. case CURL_TELQUAL_SEND:
  646. infof(data, " SEND");
  647. break;
  648. case CURL_TELQUAL_INFO:
  649. infof(data, " INFO/REPLY");
  650. break;
  651. case CURL_TELQUAL_NAME:
  652. infof(data, " NAME");
  653. break;
  654. }
  655. switch(pointer[0]) {
  656. case CURL_TELOPT_TTYPE:
  657. case CURL_TELOPT_XDISPLOC:
  658. pointer[length] = 0;
  659. infof(data, " \"%s\"", &pointer[2]);
  660. break;
  661. case CURL_TELOPT_NEW_ENVIRON:
  662. if(pointer[1] == CURL_TELQUAL_IS) {
  663. infof(data, " ");
  664. for(i = 3; i < length; i++) {
  665. switch(pointer[i]) {
  666. case CURL_NEW_ENV_VAR:
  667. infof(data, ", ");
  668. break;
  669. case CURL_NEW_ENV_VALUE:
  670. infof(data, " = ");
  671. break;
  672. default:
  673. infof(data, "%c", pointer[i]);
  674. break;
  675. }
  676. }
  677. }
  678. break;
  679. default:
  680. for(i = 2; i < length; i++)
  681. infof(data, " %.2x", pointer[i]);
  682. break;
  683. }
  684. }
  685. }
  686. }
  687. static CURLcode check_telnet_options(struct Curl_easy *data)
  688. {
  689. struct curl_slist *head;
  690. struct curl_slist *beg;
  691. char option_keyword[128] = "";
  692. char option_arg[256] = "";
  693. struct TELNET *tn = data->req.p.telnet;
  694. struct connectdata *conn = data->conn;
  695. CURLcode result = CURLE_OK;
  696. int binary_option;
  697. /* Add the user name as an environment variable if it
  698. was given on the command line */
  699. if(data->state.aptr.user) {
  700. msnprintf(option_arg, sizeof(option_arg), "USER,%s", conn->user);
  701. beg = curl_slist_append(tn->telnet_vars, option_arg);
  702. if(!beg) {
  703. curl_slist_free_all(tn->telnet_vars);
  704. tn->telnet_vars = NULL;
  705. return CURLE_OUT_OF_MEMORY;
  706. }
  707. tn->telnet_vars = beg;
  708. tn->us_preferred[CURL_TELOPT_NEW_ENVIRON] = CURL_YES;
  709. }
  710. for(head = data->set.telnet_options; head; head = head->next) {
  711. if(sscanf(head->data, "%127[^= ]%*[ =]%255s",
  712. option_keyword, option_arg) == 2) {
  713. /* Terminal type */
  714. if(strcasecompare(option_keyword, "TTYPE")) {
  715. strncpy(tn->subopt_ttype, option_arg, 31);
  716. tn->subopt_ttype[31] = 0; /* String termination */
  717. tn->us_preferred[CURL_TELOPT_TTYPE] = CURL_YES;
  718. continue;
  719. }
  720. /* Display variable */
  721. if(strcasecompare(option_keyword, "XDISPLOC")) {
  722. strncpy(tn->subopt_xdisploc, option_arg, 127);
  723. tn->subopt_xdisploc[127] = 0; /* String termination */
  724. tn->us_preferred[CURL_TELOPT_XDISPLOC] = CURL_YES;
  725. continue;
  726. }
  727. /* Environment variable */
  728. if(strcasecompare(option_keyword, "NEW_ENV")) {
  729. beg = curl_slist_append(tn->telnet_vars, option_arg);
  730. if(!beg) {
  731. result = CURLE_OUT_OF_MEMORY;
  732. break;
  733. }
  734. tn->telnet_vars = beg;
  735. tn->us_preferred[CURL_TELOPT_NEW_ENVIRON] = CURL_YES;
  736. continue;
  737. }
  738. /* Window Size */
  739. if(strcasecompare(option_keyword, "WS")) {
  740. if(sscanf(option_arg, "%hu%*[xX]%hu",
  741. &tn->subopt_wsx, &tn->subopt_wsy) == 2)
  742. tn->us_preferred[CURL_TELOPT_NAWS] = CURL_YES;
  743. else {
  744. failf(data, "Syntax error in telnet option: %s", head->data);
  745. result = CURLE_SETOPT_OPTION_SYNTAX;
  746. break;
  747. }
  748. continue;
  749. }
  750. /* To take care or not of the 8th bit in data exchange */
  751. if(strcasecompare(option_keyword, "BINARY")) {
  752. binary_option = atoi(option_arg);
  753. if(binary_option != 1) {
  754. tn->us_preferred[CURL_TELOPT_BINARY] = CURL_NO;
  755. tn->him_preferred[CURL_TELOPT_BINARY] = CURL_NO;
  756. }
  757. continue;
  758. }
  759. failf(data, "Unknown telnet option %s", head->data);
  760. result = CURLE_UNKNOWN_OPTION;
  761. break;
  762. }
  763. failf(data, "Syntax error in telnet option: %s", head->data);
  764. result = CURLE_SETOPT_OPTION_SYNTAX;
  765. break;
  766. }
  767. if(result) {
  768. curl_slist_free_all(tn->telnet_vars);
  769. tn->telnet_vars = NULL;
  770. }
  771. return result;
  772. }
  773. /*
  774. * suboption()
  775. *
  776. * Look at the sub-option buffer, and try to be helpful to the other
  777. * side.
  778. */
  779. static void suboption(struct Curl_easy *data)
  780. {
  781. struct curl_slist *v;
  782. unsigned char temp[2048];
  783. ssize_t bytes_written;
  784. size_t len;
  785. int err;
  786. char varname[128] = "";
  787. char varval[128] = "";
  788. struct TELNET *tn = data->req.p.telnet;
  789. struct connectdata *conn = data->conn;
  790. printsub(data, '<', (unsigned char *)tn->subbuffer, CURL_SB_LEN(tn) + 2);
  791. switch(CURL_SB_GET(tn)) {
  792. case CURL_TELOPT_TTYPE:
  793. len = strlen(tn->subopt_ttype) + 4 + 2;
  794. msnprintf((char *)temp, sizeof(temp),
  795. "%c%c%c%c%s%c%c", CURL_IAC, CURL_SB, CURL_TELOPT_TTYPE,
  796. CURL_TELQUAL_IS, tn->subopt_ttype, CURL_IAC, CURL_SE);
  797. bytes_written = swrite(conn->sock[FIRSTSOCKET], temp, len);
  798. if(bytes_written < 0) {
  799. err = SOCKERRNO;
  800. failf(data,"Sending data failed (%d)",err);
  801. }
  802. printsub(data, '>', &temp[2], len-2);
  803. break;
  804. case CURL_TELOPT_XDISPLOC:
  805. len = strlen(tn->subopt_xdisploc) + 4 + 2;
  806. msnprintf((char *)temp, sizeof(temp),
  807. "%c%c%c%c%s%c%c", CURL_IAC, CURL_SB, CURL_TELOPT_XDISPLOC,
  808. CURL_TELQUAL_IS, tn->subopt_xdisploc, CURL_IAC, CURL_SE);
  809. bytes_written = swrite(conn->sock[FIRSTSOCKET], temp, len);
  810. if(bytes_written < 0) {
  811. err = SOCKERRNO;
  812. failf(data,"Sending data failed (%d)",err);
  813. }
  814. printsub(data, '>', &temp[2], len-2);
  815. break;
  816. case CURL_TELOPT_NEW_ENVIRON:
  817. msnprintf((char *)temp, sizeof(temp),
  818. "%c%c%c%c", CURL_IAC, CURL_SB, CURL_TELOPT_NEW_ENVIRON,
  819. CURL_TELQUAL_IS);
  820. len = 4;
  821. for(v = tn->telnet_vars; v; v = v->next) {
  822. size_t tmplen = (strlen(v->data) + 1);
  823. /* Add the variable only if it fits */
  824. if(len + tmplen < (int)sizeof(temp)-6) {
  825. int rv;
  826. char sep[2] = "";
  827. varval[0] = 0;
  828. rv = sscanf(v->data, "%127[^,]%1[,]%127s", varname, sep, varval);
  829. if(rv == 1)
  830. len += msnprintf((char *)&temp[len], sizeof(temp) - len,
  831. "%c%s", CURL_NEW_ENV_VAR, varname);
  832. else if(rv >= 2)
  833. len += msnprintf((char *)&temp[len], sizeof(temp) - len,
  834. "%c%s%c%s", CURL_NEW_ENV_VAR, varname,
  835. CURL_NEW_ENV_VALUE, varval);
  836. }
  837. }
  838. msnprintf((char *)&temp[len], sizeof(temp) - len,
  839. "%c%c", CURL_IAC, CURL_SE);
  840. len += 2;
  841. bytes_written = swrite(conn->sock[FIRSTSOCKET], temp, len);
  842. if(bytes_written < 0) {
  843. err = SOCKERRNO;
  844. failf(data,"Sending data failed (%d)",err);
  845. }
  846. printsub(data, '>', &temp[2], len-2);
  847. break;
  848. }
  849. return;
  850. }
  851. /*
  852. * sendsuboption()
  853. *
  854. * Send suboption information to the server side.
  855. */
  856. static void sendsuboption(struct Curl_easy *data, int option)
  857. {
  858. ssize_t bytes_written;
  859. int err;
  860. unsigned short x, y;
  861. unsigned char *uc1, *uc2;
  862. struct TELNET *tn = data->req.p.telnet;
  863. struct connectdata *conn = data->conn;
  864. switch(option) {
  865. case CURL_TELOPT_NAWS:
  866. /* We prepare data to be sent */
  867. CURL_SB_CLEAR(tn);
  868. CURL_SB_ACCUM(tn, CURL_IAC);
  869. CURL_SB_ACCUM(tn, CURL_SB);
  870. CURL_SB_ACCUM(tn, CURL_TELOPT_NAWS);
  871. /* We must deal either with little or big endian processors */
  872. /* Window size must be sent according to the 'network order' */
  873. x = htons(tn->subopt_wsx);
  874. y = htons(tn->subopt_wsy);
  875. uc1 = (unsigned char *)&x;
  876. uc2 = (unsigned char *)&y;
  877. CURL_SB_ACCUM(tn, uc1[0]);
  878. CURL_SB_ACCUM(tn, uc1[1]);
  879. CURL_SB_ACCUM(tn, uc2[0]);
  880. CURL_SB_ACCUM(tn, uc2[1]);
  881. CURL_SB_ACCUM(tn, CURL_IAC);
  882. CURL_SB_ACCUM(tn, CURL_SE);
  883. CURL_SB_TERM(tn);
  884. /* data suboption is now ready */
  885. printsub(data, '>', (unsigned char *)tn->subbuffer + 2,
  886. CURL_SB_LEN(tn)-2);
  887. /* we send the header of the suboption... */
  888. bytes_written = swrite(conn->sock[FIRSTSOCKET], tn->subbuffer, 3);
  889. if(bytes_written < 0) {
  890. err = SOCKERRNO;
  891. failf(data, "Sending data failed (%d)", err);
  892. }
  893. /* ... then the window size with the send_telnet_data() function
  894. to deal with 0xFF cases ... */
  895. send_telnet_data(data, (char *)tn->subbuffer + 3, 4);
  896. /* ... and the footer */
  897. bytes_written = swrite(conn->sock[FIRSTSOCKET], tn->subbuffer + 7, 2);
  898. if(bytes_written < 0) {
  899. err = SOCKERRNO;
  900. failf(data, "Sending data failed (%d)", err);
  901. }
  902. break;
  903. }
  904. }
  905. static
  906. CURLcode telrcv(struct Curl_easy *data,
  907. const unsigned char *inbuf, /* Data received from socket */
  908. ssize_t count) /* Number of bytes received */
  909. {
  910. unsigned char c;
  911. CURLcode result;
  912. int in = 0;
  913. int startwrite = -1;
  914. struct TELNET *tn = data->req.p.telnet;
  915. #define startskipping() \
  916. if(startwrite >= 0) { \
  917. result = Curl_client_write(data, \
  918. CLIENTWRITE_BODY, \
  919. (char *)&inbuf[startwrite], \
  920. in-startwrite); \
  921. if(result) \
  922. return result; \
  923. } \
  924. startwrite = -1
  925. #define writebyte() \
  926. if(startwrite < 0) \
  927. startwrite = in
  928. #define bufferflush() startskipping()
  929. while(count--) {
  930. c = inbuf[in];
  931. switch(tn->telrcv_state) {
  932. case CURL_TS_CR:
  933. tn->telrcv_state = CURL_TS_DATA;
  934. if(c == '\0') {
  935. startskipping();
  936. break; /* Ignore \0 after CR */
  937. }
  938. writebyte();
  939. break;
  940. case CURL_TS_DATA:
  941. if(c == CURL_IAC) {
  942. tn->telrcv_state = CURL_TS_IAC;
  943. startskipping();
  944. break;
  945. }
  946. else if(c == '\r')
  947. tn->telrcv_state = CURL_TS_CR;
  948. writebyte();
  949. break;
  950. case CURL_TS_IAC:
  951. process_iac:
  952. DEBUGASSERT(startwrite < 0);
  953. switch(c) {
  954. case CURL_WILL:
  955. tn->telrcv_state = CURL_TS_WILL;
  956. break;
  957. case CURL_WONT:
  958. tn->telrcv_state = CURL_TS_WONT;
  959. break;
  960. case CURL_DO:
  961. tn->telrcv_state = CURL_TS_DO;
  962. break;
  963. case CURL_DONT:
  964. tn->telrcv_state = CURL_TS_DONT;
  965. break;
  966. case CURL_SB:
  967. CURL_SB_CLEAR(tn);
  968. tn->telrcv_state = CURL_TS_SB;
  969. break;
  970. case CURL_IAC:
  971. tn->telrcv_state = CURL_TS_DATA;
  972. writebyte();
  973. break;
  974. case CURL_DM:
  975. case CURL_NOP:
  976. case CURL_GA:
  977. default:
  978. tn->telrcv_state = CURL_TS_DATA;
  979. printoption(data, "RCVD", CURL_IAC, c);
  980. break;
  981. }
  982. break;
  983. case CURL_TS_WILL:
  984. printoption(data, "RCVD", CURL_WILL, c);
  985. tn->please_negotiate = 1;
  986. rec_will(data, c);
  987. tn->telrcv_state = CURL_TS_DATA;
  988. break;
  989. case CURL_TS_WONT:
  990. printoption(data, "RCVD", CURL_WONT, c);
  991. tn->please_negotiate = 1;
  992. rec_wont(data, c);
  993. tn->telrcv_state = CURL_TS_DATA;
  994. break;
  995. case CURL_TS_DO:
  996. printoption(data, "RCVD", CURL_DO, c);
  997. tn->please_negotiate = 1;
  998. rec_do(data, c);
  999. tn->telrcv_state = CURL_TS_DATA;
  1000. break;
  1001. case CURL_TS_DONT:
  1002. printoption(data, "RCVD", CURL_DONT, c);
  1003. tn->please_negotiate = 1;
  1004. rec_dont(data, c);
  1005. tn->telrcv_state = CURL_TS_DATA;
  1006. break;
  1007. case CURL_TS_SB:
  1008. if(c == CURL_IAC)
  1009. tn->telrcv_state = CURL_TS_SE;
  1010. else
  1011. CURL_SB_ACCUM(tn, c);
  1012. break;
  1013. case CURL_TS_SE:
  1014. if(c != CURL_SE) {
  1015. if(c != CURL_IAC) {
  1016. /*
  1017. * This is an error. We only expect to get "IAC IAC" or "IAC SE".
  1018. * Several things may have happened. An IAC was not doubled, the
  1019. * IAC SE was left off, or another option got inserted into the
  1020. * suboption are all possibilities. If we assume that the IAC was
  1021. * not doubled, and really the IAC SE was left off, we could get
  1022. * into an infinite loop here. So, instead, we terminate the
  1023. * suboption, and process the partial suboption if we can.
  1024. */
  1025. CURL_SB_ACCUM(tn, CURL_IAC);
  1026. CURL_SB_ACCUM(tn, c);
  1027. tn->subpointer -= 2;
  1028. CURL_SB_TERM(tn);
  1029. printoption(data, "In SUBOPTION processing, RCVD", CURL_IAC, c);
  1030. suboption(data); /* handle sub-option */
  1031. tn->telrcv_state = CURL_TS_IAC;
  1032. goto process_iac;
  1033. }
  1034. CURL_SB_ACCUM(tn, c);
  1035. tn->telrcv_state = CURL_TS_SB;
  1036. }
  1037. else {
  1038. CURL_SB_ACCUM(tn, CURL_IAC);
  1039. CURL_SB_ACCUM(tn, CURL_SE);
  1040. tn->subpointer -= 2;
  1041. CURL_SB_TERM(tn);
  1042. suboption(data); /* handle sub-option */
  1043. tn->telrcv_state = CURL_TS_DATA;
  1044. }
  1045. break;
  1046. }
  1047. ++in;
  1048. }
  1049. bufferflush();
  1050. return CURLE_OK;
  1051. }
  1052. /* Escape and send a telnet data block */
  1053. static CURLcode send_telnet_data(struct Curl_easy *data,
  1054. char *buffer, ssize_t nread)
  1055. {
  1056. ssize_t escapes, i, outlen;
  1057. unsigned char *outbuf = NULL;
  1058. CURLcode result = CURLE_OK;
  1059. ssize_t bytes_written, total_written;
  1060. struct connectdata *conn = data->conn;
  1061. /* Determine size of new buffer after escaping */
  1062. escapes = 0;
  1063. for(i = 0; i < nread; i++)
  1064. if((unsigned char)buffer[i] == CURL_IAC)
  1065. escapes++;
  1066. outlen = nread + escapes;
  1067. if(outlen == nread)
  1068. outbuf = (unsigned char *)buffer;
  1069. else {
  1070. ssize_t j;
  1071. outbuf = malloc(nread + escapes + 1);
  1072. if(!outbuf)
  1073. return CURLE_OUT_OF_MEMORY;
  1074. j = 0;
  1075. for(i = 0; i < nread; i++) {
  1076. outbuf[j++] = (unsigned char)buffer[i];
  1077. if((unsigned char)buffer[i] == CURL_IAC)
  1078. outbuf[j++] = CURL_IAC;
  1079. }
  1080. outbuf[j] = '\0';
  1081. }
  1082. total_written = 0;
  1083. while(!result && total_written < outlen) {
  1084. /* Make sure socket is writable to avoid EWOULDBLOCK condition */
  1085. struct pollfd pfd[1];
  1086. pfd[0].fd = conn->sock[FIRSTSOCKET];
  1087. pfd[0].events = POLLOUT;
  1088. switch(Curl_poll(pfd, 1, -1)) {
  1089. case -1: /* error, abort writing */
  1090. case 0: /* timeout (will never happen) */
  1091. result = CURLE_SEND_ERROR;
  1092. break;
  1093. default: /* write! */
  1094. bytes_written = 0;
  1095. result = Curl_write(data, conn->sock[FIRSTSOCKET],
  1096. outbuf + total_written,
  1097. outlen - total_written,
  1098. &bytes_written);
  1099. total_written += bytes_written;
  1100. break;
  1101. }
  1102. }
  1103. /* Free malloc copy if escaped */
  1104. if(outbuf != (unsigned char *)buffer)
  1105. free(outbuf);
  1106. return result;
  1107. }
  1108. static CURLcode telnet_done(struct Curl_easy *data,
  1109. CURLcode status, bool premature)
  1110. {
  1111. struct TELNET *tn = data->req.p.telnet;
  1112. (void)status; /* unused */
  1113. (void)premature; /* not used */
  1114. if(!tn)
  1115. return CURLE_OK;
  1116. curl_slist_free_all(tn->telnet_vars);
  1117. tn->telnet_vars = NULL;
  1118. Curl_safefree(data->req.p.telnet);
  1119. return CURLE_OK;
  1120. }
  1121. static CURLcode telnet_do(struct Curl_easy *data, bool *done)
  1122. {
  1123. CURLcode result;
  1124. struct connectdata *conn = data->conn;
  1125. curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
  1126. #ifdef USE_WINSOCK
  1127. WSAEVENT event_handle;
  1128. WSANETWORKEVENTS events;
  1129. HANDLE stdin_handle;
  1130. HANDLE objs[2];
  1131. DWORD obj_count;
  1132. DWORD wait_timeout;
  1133. DWORD readfile_read;
  1134. int err;
  1135. #else
  1136. timediff_t interval_ms;
  1137. struct pollfd pfd[2];
  1138. int poll_cnt;
  1139. curl_off_t total_dl = 0;
  1140. curl_off_t total_ul = 0;
  1141. #endif
  1142. ssize_t nread;
  1143. struct curltime now;
  1144. bool keepon = TRUE;
  1145. char *buf = data->state.buffer;
  1146. struct TELNET *tn;
  1147. *done = TRUE; /* unconditionally */
  1148. result = init_telnet(data);
  1149. if(result)
  1150. return result;
  1151. tn = data->req.p.telnet;
  1152. result = check_telnet_options(data);
  1153. if(result)
  1154. return result;
  1155. #ifdef USE_WINSOCK
  1156. /* We want to wait for both stdin and the socket. Since
  1157. ** the select() function in winsock only works on sockets
  1158. ** we have to use the WaitForMultipleObjects() call.
  1159. */
  1160. /* First, create a sockets event object */
  1161. event_handle = WSACreateEvent();
  1162. if(event_handle == WSA_INVALID_EVENT) {
  1163. failf(data, "WSACreateEvent failed (%d)", SOCKERRNO);
  1164. return CURLE_FAILED_INIT;
  1165. }
  1166. /* Tell winsock what events we want to listen to */
  1167. if(WSAEventSelect(sockfd, event_handle, FD_READ|FD_CLOSE) == SOCKET_ERROR) {
  1168. WSACloseEvent(event_handle);
  1169. return CURLE_OK;
  1170. }
  1171. /* The get the Windows file handle for stdin */
  1172. stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
  1173. /* Create the list of objects to wait for */
  1174. objs[0] = event_handle;
  1175. objs[1] = stdin_handle;
  1176. /* If stdin_handle is a pipe, use PeekNamedPipe() method to check it,
  1177. else use the old WaitForMultipleObjects() way */
  1178. if(GetFileType(stdin_handle) == FILE_TYPE_PIPE ||
  1179. data->set.is_fread_set) {
  1180. /* Don't wait for stdin_handle, just wait for event_handle */
  1181. obj_count = 1;
  1182. /* Check stdin_handle per 100 milliseconds */
  1183. wait_timeout = 100;
  1184. }
  1185. else {
  1186. obj_count = 2;
  1187. wait_timeout = 1000;
  1188. }
  1189. /* Keep on listening and act on events */
  1190. while(keepon) {
  1191. const DWORD buf_size = (DWORD)data->set.buffer_size;
  1192. DWORD waitret = WaitForMultipleObjects(obj_count, objs,
  1193. FALSE, wait_timeout);
  1194. switch(waitret) {
  1195. case WAIT_TIMEOUT:
  1196. {
  1197. for(;;) {
  1198. if(data->set.is_fread_set) {
  1199. size_t n;
  1200. /* read from user-supplied method */
  1201. n = data->state.fread_func(buf, 1, buf_size, data->state.in);
  1202. if(n == CURL_READFUNC_ABORT) {
  1203. keepon = FALSE;
  1204. result = CURLE_READ_ERROR;
  1205. break;
  1206. }
  1207. if(n == CURL_READFUNC_PAUSE)
  1208. break;
  1209. if(n == 0) /* no bytes */
  1210. break;
  1211. /* fall through with number of bytes read */
  1212. readfile_read = (DWORD)n;
  1213. }
  1214. else {
  1215. /* read from stdin */
  1216. if(!PeekNamedPipe(stdin_handle, NULL, 0, NULL,
  1217. &readfile_read, NULL)) {
  1218. keepon = FALSE;
  1219. result = CURLE_READ_ERROR;
  1220. break;
  1221. }
  1222. if(!readfile_read)
  1223. break;
  1224. if(!ReadFile(stdin_handle, buf, buf_size,
  1225. &readfile_read, NULL)) {
  1226. keepon = FALSE;
  1227. result = CURLE_READ_ERROR;
  1228. break;
  1229. }
  1230. }
  1231. result = send_telnet_data(data, buf, readfile_read);
  1232. if(result) {
  1233. keepon = FALSE;
  1234. break;
  1235. }
  1236. }
  1237. }
  1238. break;
  1239. case WAIT_OBJECT_0 + 1:
  1240. {
  1241. if(!ReadFile(stdin_handle, buf, buf_size,
  1242. &readfile_read, NULL)) {
  1243. keepon = FALSE;
  1244. result = CURLE_READ_ERROR;
  1245. break;
  1246. }
  1247. result = send_telnet_data(data, buf, readfile_read);
  1248. if(result) {
  1249. keepon = FALSE;
  1250. break;
  1251. }
  1252. }
  1253. break;
  1254. case WAIT_OBJECT_0:
  1255. {
  1256. events.lNetworkEvents = 0;
  1257. if(WSAEnumNetworkEvents(sockfd, event_handle, &events) == SOCKET_ERROR) {
  1258. err = SOCKERRNO;
  1259. if(err != EINPROGRESS) {
  1260. infof(data, "WSAEnumNetworkEvents failed (%d)", err);
  1261. keepon = FALSE;
  1262. result = CURLE_READ_ERROR;
  1263. }
  1264. break;
  1265. }
  1266. if(events.lNetworkEvents & FD_READ) {
  1267. /* read data from network */
  1268. result = Curl_read(data, sockfd, buf, data->set.buffer_size, &nread);
  1269. /* read would've blocked. Loop again */
  1270. if(result == CURLE_AGAIN)
  1271. break;
  1272. /* returned not-zero, this an error */
  1273. else if(result) {
  1274. keepon = FALSE;
  1275. break;
  1276. }
  1277. /* returned zero but actually received 0 or less here,
  1278. the server closed the connection and we bail out */
  1279. else if(nread <= 0) {
  1280. keepon = FALSE;
  1281. break;
  1282. }
  1283. result = telrcv(data, (unsigned char *) buf, nread);
  1284. if(result) {
  1285. keepon = FALSE;
  1286. break;
  1287. }
  1288. /* Negotiate if the peer has started negotiating,
  1289. otherwise don't. We don't want to speak telnet with
  1290. non-telnet servers, like POP or SMTP. */
  1291. if(tn->please_negotiate && !tn->already_negotiated) {
  1292. negotiate(data);
  1293. tn->already_negotiated = 1;
  1294. }
  1295. }
  1296. if(events.lNetworkEvents & FD_CLOSE) {
  1297. keepon = FALSE;
  1298. }
  1299. }
  1300. break;
  1301. }
  1302. if(data->set.timeout) {
  1303. now = Curl_now();
  1304. if(Curl_timediff(now, conn->created) >= data->set.timeout) {
  1305. failf(data, "Time-out");
  1306. result = CURLE_OPERATION_TIMEDOUT;
  1307. keepon = FALSE;
  1308. }
  1309. }
  1310. }
  1311. /* We called WSACreateEvent, so call WSACloseEvent */
  1312. if(!WSACloseEvent(event_handle)) {
  1313. infof(data, "WSACloseEvent failed (%d)", SOCKERRNO);
  1314. }
  1315. #else
  1316. pfd[0].fd = sockfd;
  1317. pfd[0].events = POLLIN;
  1318. if(data->set.is_fread_set) {
  1319. poll_cnt = 1;
  1320. interval_ms = 100; /* poll user-supplied read function */
  1321. }
  1322. else {
  1323. /* really using fread, so infile is a FILE* */
  1324. pfd[1].fd = fileno((FILE *)data->state.in);
  1325. pfd[1].events = POLLIN;
  1326. poll_cnt = 2;
  1327. interval_ms = 1 * 1000;
  1328. }
  1329. while(keepon) {
  1330. switch(Curl_poll(pfd, poll_cnt, interval_ms)) {
  1331. case -1: /* error, stop reading */
  1332. keepon = FALSE;
  1333. continue;
  1334. case 0: /* timeout */
  1335. pfd[0].revents = 0;
  1336. pfd[1].revents = 0;
  1337. /* FALLTHROUGH */
  1338. default: /* read! */
  1339. if(pfd[0].revents & POLLIN) {
  1340. /* read data from network */
  1341. result = Curl_read(data, sockfd, buf, data->set.buffer_size, &nread);
  1342. /* read would've blocked. Loop again */
  1343. if(result == CURLE_AGAIN)
  1344. break;
  1345. /* returned not-zero, this an error */
  1346. if(result) {
  1347. keepon = FALSE;
  1348. break;
  1349. }
  1350. /* returned zero but actually received 0 or less here,
  1351. the server closed the connection and we bail out */
  1352. else if(nread <= 0) {
  1353. keepon = FALSE;
  1354. break;
  1355. }
  1356. total_dl += nread;
  1357. Curl_pgrsSetDownloadCounter(data, total_dl);
  1358. result = telrcv(data, (unsigned char *)buf, nread);
  1359. if(result) {
  1360. keepon = FALSE;
  1361. break;
  1362. }
  1363. /* Negotiate if the peer has started negotiating,
  1364. otherwise don't. We don't want to speak telnet with
  1365. non-telnet servers, like POP or SMTP. */
  1366. if(tn->please_negotiate && !tn->already_negotiated) {
  1367. negotiate(data);
  1368. tn->already_negotiated = 1;
  1369. }
  1370. }
  1371. nread = 0;
  1372. if(poll_cnt == 2) {
  1373. if(pfd[1].revents & POLLIN) { /* read from in file */
  1374. nread = read(pfd[1].fd, buf, data->set.buffer_size);
  1375. }
  1376. }
  1377. else {
  1378. /* read from user-supplied method */
  1379. nread = (int)data->state.fread_func(buf, 1, data->set.buffer_size,
  1380. data->state.in);
  1381. if(nread == CURL_READFUNC_ABORT) {
  1382. keepon = FALSE;
  1383. break;
  1384. }
  1385. if(nread == CURL_READFUNC_PAUSE)
  1386. break;
  1387. }
  1388. if(nread > 0) {
  1389. result = send_telnet_data(data, buf, nread);
  1390. if(result) {
  1391. keepon = FALSE;
  1392. break;
  1393. }
  1394. total_ul += nread;
  1395. Curl_pgrsSetUploadCounter(data, total_ul);
  1396. }
  1397. else if(nread < 0)
  1398. keepon = FALSE;
  1399. break;
  1400. } /* poll switch statement */
  1401. if(data->set.timeout) {
  1402. now = Curl_now();
  1403. if(Curl_timediff(now, conn->created) >= data->set.timeout) {
  1404. failf(data, "Time-out");
  1405. result = CURLE_OPERATION_TIMEDOUT;
  1406. keepon = FALSE;
  1407. }
  1408. }
  1409. if(Curl_pgrsUpdate(data)) {
  1410. result = CURLE_ABORTED_BY_CALLBACK;
  1411. break;
  1412. }
  1413. }
  1414. #endif
  1415. /* mark this as "no further transfer wanted" */
  1416. Curl_setup_transfer(data, -1, -1, FALSE, -1);
  1417. return result;
  1418. }
  1419. #endif