2
0

telnet.c 45 KB

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