telnet.c 43 KB

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