telnet.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2007, 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 http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * $Id$
  22. ***************************************************************************/
  23. #include "setup.h"
  24. #ifndef CURL_DISABLE_TELNET
  25. /* -- WIN32 approved -- */
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <stdarg.h>
  29. #include <stdlib.h>
  30. #include <ctype.h>
  31. #if defined(WIN32)
  32. #include <time.h>
  33. #include <io.h>
  34. #else
  35. #ifdef HAVE_SYS_SOCKET_H
  36. #include <sys/socket.h>
  37. #endif
  38. #include <netinet/in.h>
  39. #ifdef HAVE_SYS_TIME_H
  40. #include <sys/time.h>
  41. #endif
  42. #ifdef HAVE_UNISTD_H
  43. #include <unistd.h>
  44. #endif
  45. #include <netdb.h>
  46. #ifdef HAVE_ARPA_INET_H
  47. #include <arpa/inet.h>
  48. #endif
  49. #ifdef HAVE_NET_IF_H
  50. #include <net/if.h>
  51. #endif
  52. #include <sys/ioctl.h>
  53. #include <signal.h>
  54. #ifdef HAVE_SYS_PARAM_H
  55. #include <sys/param.h>
  56. #endif
  57. #endif /* WIN32 */
  58. #include "urldata.h"
  59. #include <curl/curl.h>
  60. #include "transfer.h"
  61. #include "sendf.h"
  62. #include "telnet.h"
  63. #include "connect.h"
  64. #define _MPRINTF_REPLACE /* use our functions only */
  65. #include <curl/mprintf.h>
  66. #define TELOPTS
  67. #define TELCMDS
  68. #include "arpa_telnet.h"
  69. #include "memory.h"
  70. #include "select.h"
  71. /* The last #include file should be: */
  72. #include "memdebug.h"
  73. #define SUBBUFSIZE 512
  74. #define CURL_SB_CLEAR(x) x->subpointer = x->subbuffer;
  75. #define CURL_SB_TERM(x) { x->subend = x->subpointer; CURL_SB_CLEAR(x); }
  76. #define CURL_SB_ACCUM(x,c) \
  77. if(x->subpointer < (x->subbuffer+sizeof x->subbuffer)) { \
  78. *x->subpointer++ = (c); \
  79. }
  80. #define CURL_SB_GET(x) ((*x->subpointer++)&0xff)
  81. #define CURL_SB_PEEK(x) ((*x->subpointer)&0xff)
  82. #define CURL_SB_EOF(x) (x->subpointer >= x->subend)
  83. #define CURL_SB_LEN(x) (x->subend - x->subpointer)
  84. #ifdef CURL_DISABLE_VERBOSE_STRINGS
  85. #define printoption(a,b,c,d) do { } while(0)
  86. #endif
  87. #ifdef USE_WINSOCK
  88. typedef FARPROC WSOCK2_FUNC;
  89. static CURLcode check_wsock2 ( struct SessionHandle *data );
  90. #endif
  91. static
  92. void telrcv(struct connectdata *,
  93. const unsigned char *inbuf, /* Data received from socket */
  94. ssize_t count); /* Number of bytes received */
  95. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  96. static void printoption(struct SessionHandle *data,
  97. const char *direction,
  98. int cmd, int option);
  99. #endif
  100. static void negotiate(struct connectdata *);
  101. static void send_negotiation(struct connectdata *, int cmd, int option);
  102. static void set_local_option(struct connectdata *, int cmd, int option);
  103. static void set_remote_option(struct connectdata *, int cmd, int option);
  104. static void printsub(struct SessionHandle *data,
  105. int direction, unsigned char *pointer,
  106. size_t length);
  107. static void suboption(struct connectdata *);
  108. static CURLcode telnet_do(struct connectdata *conn, bool *done);
  109. static CURLcode telnet_done(struct connectdata *conn,
  110. CURLcode, bool premature);
  111. /* For negotiation compliant to RFC 1143 */
  112. #define CURL_NO 0
  113. #define CURL_YES 1
  114. #define CURL_WANTYES 2
  115. #define CURL_WANTNO 3
  116. #define CURL_EMPTY 0
  117. #define CURL_OPPOSITE 1
  118. /*
  119. * Telnet receiver states for fsm
  120. */
  121. typedef enum
  122. {
  123. CURL_TS_DATA = 0,
  124. CURL_TS_IAC,
  125. CURL_TS_WILL,
  126. CURL_TS_WONT,
  127. CURL_TS_DO,
  128. CURL_TS_DONT,
  129. CURL_TS_CR,
  130. CURL_TS_SB, /* sub-option collection */
  131. CURL_TS_SE /* looking for sub-option end */
  132. } TelnetReceive;
  133. struct TELNET {
  134. int please_negotiate;
  135. int already_negotiated;
  136. int us[256];
  137. int usq[256];
  138. int us_preferred[256];
  139. int him[256];
  140. int himq[256];
  141. int him_preferred[256];
  142. char subopt_ttype[32]; /* Set with suboption TTYPE */
  143. char subopt_xdisploc[128]; /* Set with suboption XDISPLOC */
  144. struct curl_slist *telnet_vars; /* Environment variables */
  145. /* suboptions */
  146. unsigned char subbuffer[SUBBUFSIZE];
  147. unsigned char *subpointer, *subend; /* buffer for sub-options */
  148. TelnetReceive telrcv_state;
  149. };
  150. /*
  151. * TELNET protocol handler.
  152. */
  153. const struct Curl_handler Curl_handler_telnet = {
  154. "TELNET", /* scheme */
  155. ZERO_NULL, /* setup_connection */
  156. telnet_do, /* do_it */
  157. telnet_done, /* done */
  158. ZERO_NULL, /* do_more */
  159. ZERO_NULL, /* connect_it */
  160. ZERO_NULL, /* connecting */
  161. ZERO_NULL, /* doing */
  162. ZERO_NULL, /* proto_getsock */
  163. ZERO_NULL, /* doing_getsock */
  164. ZERO_NULL, /* disconnect */
  165. PORT_TELNET, /* defport */
  166. PROT_TELNET /* protocol */
  167. };
  168. #ifdef USE_WINSOCK
  169. static CURLcode
  170. check_wsock2 ( struct SessionHandle *data )
  171. {
  172. int err;
  173. WORD wVersionRequested;
  174. WSADATA wsaData;
  175. DEBUGASSERT(data);
  176. /* telnet requires at least WinSock 2.0 so ask for it. */
  177. wVersionRequested = MAKEWORD(2, 0);
  178. err = WSAStartup(wVersionRequested, &wsaData);
  179. /* We must've called this once already, so this call */
  180. /* should always succeed. But, just in case... */
  181. if(err != 0) {
  182. failf(data,"WSAStartup failed (%d)",err);
  183. return CURLE_FAILED_INIT;
  184. }
  185. /* We have to have a WSACleanup call for every successful */
  186. /* WSAStartup call. */
  187. WSACleanup();
  188. /* Check that our version is supported */
  189. if(LOBYTE(wsaData.wVersion) != LOBYTE(wVersionRequested) ||
  190. HIBYTE(wsaData.wVersion) != HIBYTE(wVersionRequested)) {
  191. /* Our version isn't supported */
  192. failf(data,"insufficient winsock version to support "
  193. "telnet");
  194. return CURLE_FAILED_INIT;
  195. }
  196. /* Our version is supported */
  197. return CURLE_OK;
  198. }
  199. #endif
  200. static
  201. CURLcode init_telnet(struct connectdata *conn)
  202. {
  203. struct TELNET *tn;
  204. tn = (struct TELNET *)calloc(1, sizeof(struct TELNET));
  205. if(!tn)
  206. return CURLE_OUT_OF_MEMORY;
  207. conn->data->state.proto.telnet = (void *)tn; /* make us known */
  208. tn->telrcv_state = CURL_TS_DATA;
  209. /* Init suboptions */
  210. CURL_SB_CLEAR(tn);
  211. /* Set the options we want by default */
  212. tn->us_preferred[CURL_TELOPT_BINARY] = CURL_YES;
  213. tn->us_preferred[CURL_TELOPT_SGA] = CURL_YES;
  214. tn->him_preferred[CURL_TELOPT_BINARY] = CURL_YES;
  215. tn->him_preferred[CURL_TELOPT_SGA] = CURL_YES;
  216. return CURLE_OK;
  217. }
  218. static void negotiate(struct connectdata *conn)
  219. {
  220. int i;
  221. struct TELNET *tn = (struct TELNET *) conn->data->state.proto.telnet;
  222. for(i = 0;i < CURL_NTELOPTS;i++)
  223. {
  224. if(tn->us_preferred[i] == CURL_YES)
  225. set_local_option(conn, i, CURL_YES);
  226. if(tn->him_preferred[i] == CURL_YES)
  227. set_remote_option(conn, i, CURL_YES);
  228. }
  229. }
  230. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  231. static void printoption(struct SessionHandle *data,
  232. const char *direction, int cmd, int option)
  233. {
  234. const char *fmt;
  235. const char *opt;
  236. if(data->set.verbose)
  237. {
  238. if(cmd == CURL_IAC)
  239. {
  240. if(CURL_TELCMD_OK(option))
  241. infof(data, "%s IAC %s\n", direction, CURL_TELCMD(option));
  242. else
  243. infof(data, "%s IAC %d\n", direction, option);
  244. }
  245. else
  246. {
  247. fmt = (cmd == CURL_WILL) ? "WILL" : (cmd == CURL_WONT) ? "WONT" :
  248. (cmd == CURL_DO) ? "DO" : (cmd == CURL_DONT) ? "DONT" : 0;
  249. if(fmt)
  250. {
  251. if(CURL_TELOPT_OK(option))
  252. opt = CURL_TELOPT(option);
  253. else if(option == CURL_TELOPT_EXOPL)
  254. opt = "EXOPL";
  255. else
  256. opt = NULL;
  257. if(opt)
  258. infof(data, "%s %s %s\n", direction, fmt, opt);
  259. else
  260. infof(data, "%s %s %d\n", direction, fmt, option);
  261. }
  262. else
  263. infof(data, "%s %d %d\n", direction, cmd, option);
  264. }
  265. }
  266. }
  267. #endif
  268. static void send_negotiation(struct connectdata *conn, int cmd, int option)
  269. {
  270. unsigned char buf[3];
  271. ssize_t bytes_written;
  272. int err;
  273. struct SessionHandle *data = conn->data;
  274. buf[0] = CURL_IAC;
  275. buf[1] = (unsigned char)cmd;
  276. buf[2] = (unsigned char)option;
  277. bytes_written = swrite(conn->sock[FIRSTSOCKET], buf, 3);
  278. if(bytes_written < 0) {
  279. err = SOCKERRNO;
  280. failf(data,"Sending data failed (%d)",err);
  281. }
  282. printoption(conn->data, "SENT", cmd, option);
  283. }
  284. static
  285. void set_remote_option(struct connectdata *conn, int option, int newstate)
  286. {
  287. struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
  288. if(newstate == CURL_YES)
  289. {
  290. switch(tn->him[option])
  291. {
  292. case CURL_NO:
  293. tn->him[option] = CURL_WANTYES;
  294. send_negotiation(conn, CURL_DO, option);
  295. break;
  296. case CURL_YES:
  297. /* Already enabled */
  298. break;
  299. case CURL_WANTNO:
  300. switch(tn->himq[option])
  301. {
  302. case CURL_EMPTY:
  303. /* Already negotiating for CURL_YES, queue the request */
  304. tn->himq[option] = CURL_OPPOSITE;
  305. break;
  306. case CURL_OPPOSITE:
  307. /* Error: already queued an enable request */
  308. break;
  309. }
  310. break;
  311. case CURL_WANTYES:
  312. switch(tn->himq[option])
  313. {
  314. case CURL_EMPTY:
  315. /* Error: already negotiating for enable */
  316. break;
  317. case CURL_OPPOSITE:
  318. tn->himq[option] = CURL_EMPTY;
  319. break;
  320. }
  321. break;
  322. }
  323. }
  324. else /* NO */
  325. {
  326. switch(tn->him[option])
  327. {
  328. case CURL_NO:
  329. /* Already disabled */
  330. break;
  331. case CURL_YES:
  332. tn->him[option] = CURL_WANTNO;
  333. send_negotiation(conn, CURL_DONT, option);
  334. break;
  335. case CURL_WANTNO:
  336. switch(tn->himq[option])
  337. {
  338. case CURL_EMPTY:
  339. /* Already negotiating for NO */
  340. break;
  341. case CURL_OPPOSITE:
  342. tn->himq[option] = CURL_EMPTY;
  343. break;
  344. }
  345. break;
  346. case CURL_WANTYES:
  347. switch(tn->himq[option])
  348. {
  349. case CURL_EMPTY:
  350. tn->himq[option] = CURL_OPPOSITE;
  351. break;
  352. case CURL_OPPOSITE:
  353. break;
  354. }
  355. break;
  356. }
  357. }
  358. }
  359. static
  360. void rec_will(struct connectdata *conn, int option)
  361. {
  362. struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
  363. switch(tn->him[option])
  364. {
  365. case CURL_NO:
  366. if(tn->him_preferred[option] == CURL_YES)
  367. {
  368. tn->him[option] = CURL_YES;
  369. send_negotiation(conn, CURL_DO, option);
  370. }
  371. else
  372. {
  373. send_negotiation(conn, CURL_DONT, option);
  374. }
  375. break;
  376. case CURL_YES:
  377. /* Already enabled */
  378. break;
  379. case CURL_WANTNO:
  380. switch(tn->himq[option])
  381. {
  382. case CURL_EMPTY:
  383. /* Error: DONT answered by WILL */
  384. tn->him[option] = CURL_NO;
  385. break;
  386. case CURL_OPPOSITE:
  387. /* Error: DONT answered by WILL */
  388. tn->him[option] = CURL_YES;
  389. tn->himq[option] = CURL_EMPTY;
  390. break;
  391. }
  392. break;
  393. case CURL_WANTYES:
  394. switch(tn->himq[option])
  395. {
  396. case CURL_EMPTY:
  397. tn->him[option] = CURL_YES;
  398. break;
  399. case CURL_OPPOSITE:
  400. tn->him[option] = CURL_WANTNO;
  401. tn->himq[option] = CURL_EMPTY;
  402. send_negotiation(conn, CURL_DONT, option);
  403. break;
  404. }
  405. break;
  406. }
  407. }
  408. static
  409. void rec_wont(struct connectdata *conn, int option)
  410. {
  411. struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
  412. switch(tn->him[option])
  413. {
  414. case CURL_NO:
  415. /* Already disabled */
  416. break;
  417. case CURL_YES:
  418. tn->him[option] = CURL_NO;
  419. send_negotiation(conn, CURL_DONT, option);
  420. break;
  421. case CURL_WANTNO:
  422. switch(tn->himq[option])
  423. {
  424. case CURL_EMPTY:
  425. tn->him[option] = CURL_NO;
  426. break;
  427. case CURL_OPPOSITE:
  428. tn->him[option] = CURL_WANTYES;
  429. tn->himq[option] = CURL_EMPTY;
  430. send_negotiation(conn, CURL_DO, option);
  431. break;
  432. }
  433. break;
  434. case CURL_WANTYES:
  435. switch(tn->himq[option])
  436. {
  437. case CURL_EMPTY:
  438. tn->him[option] = CURL_NO;
  439. break;
  440. case CURL_OPPOSITE:
  441. tn->him[option] = CURL_NO;
  442. tn->himq[option] = CURL_EMPTY;
  443. break;
  444. }
  445. break;
  446. }
  447. }
  448. static void
  449. set_local_option(struct connectdata *conn, int option, int newstate)
  450. {
  451. struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
  452. if(newstate == CURL_YES)
  453. {
  454. switch(tn->us[option])
  455. {
  456. case CURL_NO:
  457. tn->us[option] = CURL_WANTYES;
  458. send_negotiation(conn, CURL_WILL, option);
  459. break;
  460. case CURL_YES:
  461. /* Already enabled */
  462. break;
  463. case CURL_WANTNO:
  464. switch(tn->usq[option])
  465. {
  466. case CURL_EMPTY:
  467. /* Already negotiating for CURL_YES, queue the request */
  468. tn->usq[option] = CURL_OPPOSITE;
  469. break;
  470. case CURL_OPPOSITE:
  471. /* Error: already queued an enable request */
  472. break;
  473. }
  474. break;
  475. case CURL_WANTYES:
  476. switch(tn->usq[option])
  477. {
  478. case CURL_EMPTY:
  479. /* Error: already negotiating for enable */
  480. break;
  481. case CURL_OPPOSITE:
  482. tn->usq[option] = CURL_EMPTY;
  483. break;
  484. }
  485. break;
  486. }
  487. }
  488. else /* NO */
  489. {
  490. switch(tn->us[option])
  491. {
  492. case CURL_NO:
  493. /* Already disabled */
  494. break;
  495. case CURL_YES:
  496. tn->us[option] = CURL_WANTNO;
  497. send_negotiation(conn, CURL_WONT, option);
  498. break;
  499. case CURL_WANTNO:
  500. switch(tn->usq[option])
  501. {
  502. case CURL_EMPTY:
  503. /* Already negotiating for NO */
  504. break;
  505. case CURL_OPPOSITE:
  506. tn->usq[option] = CURL_EMPTY;
  507. break;
  508. }
  509. break;
  510. case CURL_WANTYES:
  511. switch(tn->usq[option])
  512. {
  513. case CURL_EMPTY:
  514. tn->usq[option] = CURL_OPPOSITE;
  515. break;
  516. case CURL_OPPOSITE:
  517. break;
  518. }
  519. break;
  520. }
  521. }
  522. }
  523. static
  524. void rec_do(struct connectdata *conn, int option)
  525. {
  526. struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
  527. switch(tn->us[option])
  528. {
  529. case CURL_NO:
  530. if(tn->us_preferred[option] == CURL_YES)
  531. {
  532. tn->us[option] = CURL_YES;
  533. send_negotiation(conn, CURL_WILL, option);
  534. }
  535. else
  536. {
  537. send_negotiation(conn, CURL_WONT, option);
  538. }
  539. break;
  540. case CURL_YES:
  541. /* Already enabled */
  542. break;
  543. case CURL_WANTNO:
  544. switch(tn->usq[option])
  545. {
  546. case CURL_EMPTY:
  547. /* Error: DONT answered by WILL */
  548. tn->us[option] = CURL_NO;
  549. break;
  550. case CURL_OPPOSITE:
  551. /* Error: DONT answered by WILL */
  552. tn->us[option] = CURL_YES;
  553. tn->usq[option] = CURL_EMPTY;
  554. break;
  555. }
  556. break;
  557. case CURL_WANTYES:
  558. switch(tn->usq[option])
  559. {
  560. case CURL_EMPTY:
  561. tn->us[option] = CURL_YES;
  562. break;
  563. case CURL_OPPOSITE:
  564. tn->us[option] = CURL_WANTNO;
  565. tn->himq[option] = CURL_EMPTY;
  566. send_negotiation(conn, CURL_WONT, option);
  567. break;
  568. }
  569. break;
  570. }
  571. }
  572. static
  573. void rec_dont(struct connectdata *conn, int option)
  574. {
  575. struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
  576. switch(tn->us[option])
  577. {
  578. case CURL_NO:
  579. /* Already disabled */
  580. break;
  581. case CURL_YES:
  582. tn->us[option] = CURL_NO;
  583. send_negotiation(conn, CURL_WONT, option);
  584. break;
  585. case CURL_WANTNO:
  586. switch(tn->usq[option])
  587. {
  588. case CURL_EMPTY:
  589. tn->us[option] = CURL_NO;
  590. break;
  591. case CURL_OPPOSITE:
  592. tn->us[option] = CURL_WANTYES;
  593. tn->usq[option] = CURL_EMPTY;
  594. send_negotiation(conn, CURL_WILL, option);
  595. break;
  596. }
  597. break;
  598. case CURL_WANTYES:
  599. switch(tn->usq[option])
  600. {
  601. case CURL_EMPTY:
  602. tn->us[option] = CURL_NO;
  603. break;
  604. case CURL_OPPOSITE:
  605. tn->us[option] = CURL_NO;
  606. tn->usq[option] = CURL_EMPTY;
  607. break;
  608. }
  609. break;
  610. }
  611. }
  612. static void printsub(struct SessionHandle *data,
  613. int direction, /* '<' or '>' */
  614. unsigned char *pointer, /* where suboption data is */
  615. size_t length) /* length of suboption data */
  616. {
  617. unsigned int i = 0;
  618. if(data->set.verbose)
  619. {
  620. if(direction)
  621. {
  622. infof(data, "%s IAC SB ", (direction == '<')? "RCVD":"SENT");
  623. if(length >= 3)
  624. {
  625. int j;
  626. i = pointer[length-2];
  627. j = pointer[length-1];
  628. if(i != CURL_IAC || j != CURL_SE)
  629. {
  630. infof(data, "(terminated by ");
  631. if(CURL_TELOPT_OK(i))
  632. infof(data, "%s ", CURL_TELOPT(i));
  633. else if(CURL_TELCMD_OK(i))
  634. infof(data, "%s ", CURL_TELCMD(i));
  635. else
  636. infof(data, "%d ", i);
  637. if(CURL_TELOPT_OK(j))
  638. infof(data, "%s", CURL_TELOPT(j));
  639. else if(CURL_TELCMD_OK(j))
  640. infof(data, "%s", CURL_TELCMD(j));
  641. else
  642. infof(data, "%d", j);
  643. infof(data, ", not IAC SE!) ");
  644. }
  645. }
  646. length -= 2;
  647. }
  648. if(length < 1)
  649. {
  650. infof(data, "(Empty suboption?)");
  651. return;
  652. }
  653. if(CURL_TELOPT_OK(pointer[0])) {
  654. switch(pointer[0]) {
  655. case CURL_TELOPT_TTYPE:
  656. case CURL_TELOPT_XDISPLOC:
  657. case CURL_TELOPT_NEW_ENVIRON:
  658. infof(data, "%s", CURL_TELOPT(pointer[0]));
  659. break;
  660. default:
  661. infof(data, "%s (unsupported)", CURL_TELOPT(pointer[0]));
  662. break;
  663. }
  664. }
  665. else
  666. infof(data, "%d (unknown)", pointer[i]);
  667. switch(pointer[1]) {
  668. case CURL_TELQUAL_IS:
  669. infof(data, " IS");
  670. break;
  671. case CURL_TELQUAL_SEND:
  672. infof(data, " SEND");
  673. break;
  674. case CURL_TELQUAL_INFO:
  675. infof(data, " INFO/REPLY");
  676. break;
  677. case CURL_TELQUAL_NAME:
  678. infof(data, " NAME");
  679. break;
  680. }
  681. switch(pointer[0]) {
  682. case CURL_TELOPT_TTYPE:
  683. case CURL_TELOPT_XDISPLOC:
  684. pointer[length] = 0;
  685. infof(data, " \"%s\"", &pointer[2]);
  686. break;
  687. case CURL_TELOPT_NEW_ENVIRON:
  688. if(pointer[1] == CURL_TELQUAL_IS) {
  689. infof(data, " ");
  690. for(i = 3;i < length;i++) {
  691. switch(pointer[i]) {
  692. case CURL_NEW_ENV_VAR:
  693. infof(data, ", ");
  694. break;
  695. case CURL_NEW_ENV_VALUE:
  696. infof(data, " = ");
  697. break;
  698. default:
  699. infof(data, "%c", pointer[i]);
  700. break;
  701. }
  702. }
  703. }
  704. break;
  705. default:
  706. for (i = 2; i < length; i++)
  707. infof(data, " %.2x", pointer[i]);
  708. break;
  709. }
  710. if(direction)
  711. {
  712. infof(data, "\n");
  713. }
  714. }
  715. }
  716. static CURLcode check_telnet_options(struct connectdata *conn)
  717. {
  718. struct curl_slist *head;
  719. char option_keyword[128];
  720. char option_arg[256];
  721. char *buf;
  722. struct SessionHandle *data = conn->data;
  723. struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
  724. /* Add the user name as an environment variable if it
  725. was given on the command line */
  726. if(conn->bits.user_passwd)
  727. {
  728. snprintf(option_arg, sizeof(option_arg), "USER,%s", conn->user);
  729. tn->telnet_vars = curl_slist_append(tn->telnet_vars, option_arg);
  730. tn->us_preferred[CURL_TELOPT_NEW_ENVIRON] = CURL_YES;
  731. }
  732. for(head = data->set.telnet_options; head; head=head->next) {
  733. if(sscanf(head->data, "%127[^= ]%*[ =]%255s",
  734. option_keyword, option_arg) == 2) {
  735. /* Terminal type */
  736. if(curl_strequal(option_keyword, "TTYPE")) {
  737. strncpy(tn->subopt_ttype, option_arg, 31);
  738. tn->subopt_ttype[31] = 0; /* String termination */
  739. tn->us_preferred[CURL_TELOPT_TTYPE] = CURL_YES;
  740. continue;
  741. }
  742. /* Display variable */
  743. if(curl_strequal(option_keyword, "XDISPLOC")) {
  744. strncpy(tn->subopt_xdisploc, option_arg, 127);
  745. tn->subopt_xdisploc[127] = 0; /* String termination */
  746. tn->us_preferred[CURL_TELOPT_XDISPLOC] = CURL_YES;
  747. continue;
  748. }
  749. /* Environment variable */
  750. if(curl_strequal(option_keyword, "NEW_ENV")) {
  751. buf = strdup(option_arg);
  752. if(!buf)
  753. return CURLE_OUT_OF_MEMORY;
  754. tn->telnet_vars = curl_slist_append(tn->telnet_vars, buf);
  755. tn->us_preferred[CURL_TELOPT_NEW_ENVIRON] = CURL_YES;
  756. continue;
  757. }
  758. failf(data, "Unknown telnet option %s", head->data);
  759. return CURLE_UNKNOWN_TELNET_OPTION;
  760. } else {
  761. failf(data, "Syntax error in telnet option: %s", head->data);
  762. return CURLE_TELNET_OPTION_SYNTAX;
  763. }
  764. }
  765. return CURLE_OK;
  766. }
  767. /*
  768. * suboption()
  769. *
  770. * Look at the sub-option buffer, and try to be helpful to the other
  771. * side.
  772. */
  773. static void suboption(struct connectdata *conn)
  774. {
  775. struct curl_slist *v;
  776. unsigned char temp[2048];
  777. ssize_t bytes_written;
  778. size_t len;
  779. size_t tmplen;
  780. int err;
  781. char varname[128];
  782. char varval[128];
  783. struct SessionHandle *data = conn->data;
  784. struct TELNET *tn = (struct TELNET *)data->state.proto.telnet;
  785. printsub(data, '<', (unsigned char *)tn->subbuffer, CURL_SB_LEN(tn)+2);
  786. switch (CURL_SB_GET(tn)) {
  787. case CURL_TELOPT_TTYPE:
  788. len = strlen(tn->subopt_ttype) + 4 + 2;
  789. snprintf((char *)temp, sizeof(temp),
  790. "%c%c%c%c%s%c%c", CURL_IAC, CURL_SB, CURL_TELOPT_TTYPE,
  791. CURL_TELQUAL_IS, tn->subopt_ttype, CURL_IAC, CURL_SE);
  792. bytes_written = swrite(conn->sock[FIRSTSOCKET], temp, len);
  793. if(bytes_written < 0) {
  794. err = SOCKERRNO;
  795. failf(data,"Sending data failed (%d)",err);
  796. }
  797. printsub(data, '>', &temp[2], len-2);
  798. break;
  799. case CURL_TELOPT_XDISPLOC:
  800. len = strlen(tn->subopt_xdisploc) + 4 + 2;
  801. snprintf((char *)temp, sizeof(temp),
  802. "%c%c%c%c%s%c%c", CURL_IAC, CURL_SB, CURL_TELOPT_XDISPLOC,
  803. CURL_TELQUAL_IS, tn->subopt_xdisploc, CURL_IAC, CURL_SE);
  804. bytes_written = swrite(conn->sock[FIRSTSOCKET], temp, len);
  805. if(bytes_written < 0) {
  806. err = SOCKERRNO;
  807. failf(data,"Sending data failed (%d)",err);
  808. }
  809. printsub(data, '>', &temp[2], len-2);
  810. break;
  811. case CURL_TELOPT_NEW_ENVIRON:
  812. snprintf((char *)temp, sizeof(temp),
  813. "%c%c%c%c", CURL_IAC, CURL_SB, CURL_TELOPT_NEW_ENVIRON,
  814. CURL_TELQUAL_IS);
  815. len = 4;
  816. for(v = tn->telnet_vars;v;v = v->next) {
  817. tmplen = (strlen(v->data) + 1);
  818. /* Add the variable only if it fits */
  819. if(len + tmplen < (int)sizeof(temp)-6) {
  820. sscanf(v->data, "%127[^,],%127s", varname, varval);
  821. snprintf((char *)&temp[len], sizeof(temp) - len,
  822. "%c%s%c%s", CURL_NEW_ENV_VAR, varname,
  823. CURL_NEW_ENV_VALUE, varval);
  824. len += tmplen;
  825. }
  826. }
  827. snprintf((char *)&temp[len], sizeof(temp) - len,
  828. "%c%c", CURL_IAC, CURL_SE);
  829. len += 2;
  830. bytes_written = swrite(conn->sock[FIRSTSOCKET], temp, len);
  831. if(bytes_written < 0) {
  832. err = SOCKERRNO;
  833. failf(data,"Sending data failed (%d)",err);
  834. }
  835. printsub(data, '>', &temp[2], len-2);
  836. break;
  837. }
  838. return;
  839. }
  840. static
  841. void telrcv(struct connectdata *conn,
  842. const unsigned char *inbuf, /* Data received from socket */
  843. ssize_t count) /* Number of bytes received */
  844. {
  845. unsigned char c;
  846. int in = 0;
  847. int startwrite=-1;
  848. struct SessionHandle *data = conn->data;
  849. struct TELNET *tn = (struct TELNET *)data->state.proto.telnet;
  850. #define startskipping() \
  851. if(startwrite >= 0) \
  852. Curl_client_write(conn, CLIENTWRITE_BODY, (char *)&inbuf[startwrite], in-startwrite); \
  853. startwrite = -1
  854. #define writebyte() \
  855. if(startwrite < 0) \
  856. startwrite = in
  857. #define bufferflush() startskipping()
  858. while(count--)
  859. {
  860. c = inbuf[in];
  861. /*infof(data,"In rcv state %d char %d\n", tn->telrcv_state, c);*/
  862. switch (tn->telrcv_state)
  863. {
  864. case CURL_TS_CR:
  865. tn->telrcv_state = CURL_TS_DATA;
  866. if(c == '\0')
  867. {
  868. startskipping();
  869. break; /* Ignore \0 after CR */
  870. }
  871. writebyte();
  872. break;
  873. case CURL_TS_DATA:
  874. if(c == CURL_IAC)
  875. {
  876. tn->telrcv_state = CURL_TS_IAC;
  877. startskipping();
  878. break;
  879. }
  880. else if(c == '\r')
  881. {
  882. tn->telrcv_state = CURL_TS_CR;
  883. }
  884. writebyte();
  885. break;
  886. case CURL_TS_IAC:
  887. process_iac:
  888. DEBUGASSERT(startwrite < 0);
  889. switch (c)
  890. {
  891. case CURL_WILL:
  892. tn->telrcv_state = CURL_TS_WILL;
  893. break;
  894. case CURL_WONT:
  895. tn->telrcv_state = CURL_TS_WONT;
  896. break;
  897. case CURL_DO:
  898. tn->telrcv_state = CURL_TS_DO;
  899. break;
  900. case CURL_DONT:
  901. tn->telrcv_state = CURL_TS_DONT;
  902. break;
  903. case CURL_SB:
  904. CURL_SB_CLEAR(tn);
  905. tn->telrcv_state = CURL_TS_SB;
  906. break;
  907. case CURL_IAC:
  908. tn->telrcv_state = CURL_TS_DATA;
  909. writebyte();
  910. break;
  911. case CURL_DM:
  912. case CURL_NOP:
  913. case CURL_GA:
  914. default:
  915. tn->telrcv_state = CURL_TS_DATA;
  916. printoption(data, "RCVD", CURL_IAC, c);
  917. break;
  918. }
  919. break;
  920. case CURL_TS_WILL:
  921. printoption(data, "RCVD", CURL_WILL, c);
  922. tn->please_negotiate = 1;
  923. rec_will(conn, c);
  924. tn->telrcv_state = CURL_TS_DATA;
  925. break;
  926. case CURL_TS_WONT:
  927. printoption(data, "RCVD", CURL_WONT, c);
  928. tn->please_negotiate = 1;
  929. rec_wont(conn, c);
  930. tn->telrcv_state = CURL_TS_DATA;
  931. break;
  932. case CURL_TS_DO:
  933. printoption(data, "RCVD", CURL_DO, c);
  934. tn->please_negotiate = 1;
  935. rec_do(conn, c);
  936. tn->telrcv_state = CURL_TS_DATA;
  937. break;
  938. case CURL_TS_DONT:
  939. printoption(data, "RCVD", CURL_DONT, c);
  940. tn->please_negotiate = 1;
  941. rec_dont(conn, c);
  942. tn->telrcv_state = CURL_TS_DATA;
  943. break;
  944. case CURL_TS_SB:
  945. if(c == CURL_IAC)
  946. {
  947. tn->telrcv_state = CURL_TS_SE;
  948. }
  949. else
  950. {
  951. CURL_SB_ACCUM(tn,c);
  952. }
  953. break;
  954. case CURL_TS_SE:
  955. if(c != CURL_SE)
  956. {
  957. if(c != CURL_IAC)
  958. {
  959. /*
  960. * This is an error. We only expect to get "IAC IAC" or "IAC SE".
  961. * Several things may have happend. An IAC was not doubled, the
  962. * IAC SE was left off, or another option got inserted into the
  963. * suboption are all possibilities. If we assume that the IAC was
  964. * not doubled, and really the IAC SE was left off, we could get
  965. * into an infinate loop here. So, instead, we terminate the
  966. * suboption, and process the partial suboption if we can.
  967. */
  968. CURL_SB_ACCUM(tn, CURL_IAC);
  969. CURL_SB_ACCUM(tn, c);
  970. tn->subpointer -= 2;
  971. CURL_SB_TERM(tn);
  972. printoption(data, "In SUBOPTION processing, RCVD", CURL_IAC, c);
  973. suboption(conn); /* handle sub-option */
  974. tn->telrcv_state = CURL_TS_IAC;
  975. goto process_iac;
  976. }
  977. CURL_SB_ACCUM(tn,c);
  978. tn->telrcv_state = CURL_TS_SB;
  979. }
  980. else
  981. {
  982. CURL_SB_ACCUM(tn, CURL_IAC);
  983. CURL_SB_ACCUM(tn, CURL_SE);
  984. tn->subpointer -= 2;
  985. CURL_SB_TERM(tn);
  986. suboption(conn); /* handle sub-option */
  987. tn->telrcv_state = CURL_TS_DATA;
  988. }
  989. break;
  990. }
  991. ++in;
  992. }
  993. bufferflush();
  994. }
  995. /* Escape and send a telnet data block */
  996. /* TODO: write large chunks of data instead of one byte at a time */
  997. static CURLcode send_telnet_data(struct connectdata *conn,
  998. char *buffer, ssize_t nread)
  999. {
  1000. unsigned char outbuf[2];
  1001. ssize_t bytes_written, total_written;
  1002. int out_count;
  1003. CURLcode rc = CURLE_OK;
  1004. while(rc == CURLE_OK && nread--) {
  1005. outbuf[0] = *buffer++;
  1006. out_count = 1;
  1007. if(outbuf[0] == CURL_IAC)
  1008. outbuf[out_count++] = CURL_IAC;
  1009. total_written = 0;
  1010. do {
  1011. /* Make sure socket is writable to avoid EWOULDBLOCK condition */
  1012. struct pollfd pfd[1];
  1013. pfd[0].fd = conn->sock[FIRSTSOCKET];
  1014. pfd[0].events = POLLOUT;
  1015. switch (Curl_poll(pfd, 1, -1)) {
  1016. case -1: /* error, abort writing */
  1017. case 0: /* timeout (will never happen) */
  1018. rc = CURLE_SEND_ERROR;
  1019. break;
  1020. default: /* write! */
  1021. bytes_written = 0;
  1022. rc = Curl_write(conn, conn->sock[FIRSTSOCKET], outbuf+total_written,
  1023. out_count-total_written, &bytes_written);
  1024. total_written += bytes_written;
  1025. break;
  1026. }
  1027. /* handle partial write */
  1028. } while (rc == CURLE_OK && total_written < out_count);
  1029. }
  1030. return rc;
  1031. }
  1032. static CURLcode telnet_done(struct connectdata *conn,
  1033. CURLcode status, bool premature)
  1034. {
  1035. struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
  1036. (void)status; /* unused */
  1037. (void)premature; /* not used */
  1038. curl_slist_free_all(tn->telnet_vars);
  1039. free(conn->data->state.proto.telnet);
  1040. conn->data->state.proto.telnet = NULL;
  1041. return CURLE_OK;
  1042. }
  1043. static CURLcode telnet_do(struct connectdata *conn, bool *done)
  1044. {
  1045. CURLcode code;
  1046. struct SessionHandle *data = conn->data;
  1047. curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
  1048. #ifdef USE_WINSOCK
  1049. HMODULE wsock2;
  1050. WSOCK2_FUNC close_event_func;
  1051. WSOCK2_FUNC create_event_func;
  1052. WSOCK2_FUNC event_select_func;
  1053. WSOCK2_FUNC enum_netevents_func;
  1054. WSAEVENT event_handle;
  1055. WSANETWORKEVENTS events;
  1056. HANDLE stdin_handle;
  1057. HANDLE objs[2];
  1058. DWORD obj_count;
  1059. DWORD wait_timeout;
  1060. DWORD waitret;
  1061. DWORD readfile_read;
  1062. #else
  1063. int interval_ms;
  1064. struct pollfd pfd[2];
  1065. #endif
  1066. ssize_t nread;
  1067. bool keepon = TRUE;
  1068. char *buf = data->state.buffer;
  1069. struct TELNET *tn;
  1070. *done = TRUE; /* unconditionally */
  1071. code = init_telnet(conn);
  1072. if(code)
  1073. return code;
  1074. tn = (struct TELNET *)data->state.proto.telnet;
  1075. code = check_telnet_options(conn);
  1076. if(code)
  1077. return code;
  1078. #ifdef USE_WINSOCK
  1079. /*
  1080. ** This functionality only works with WinSock >= 2.0. So,
  1081. ** make sure have it.
  1082. */
  1083. code = check_wsock2(data);
  1084. if(code)
  1085. return code;
  1086. /* OK, so we have WinSock 2.0. We need to dynamically */
  1087. /* load ws2_32.dll and get the function pointers we need. */
  1088. wsock2 = LoadLibrary("WS2_32.DLL");
  1089. if(wsock2 == NULL) {
  1090. failf(data,"failed to load WS2_32.DLL (%d)", ERRNO);
  1091. return CURLE_FAILED_INIT;
  1092. }
  1093. /* Grab a pointer to WSACreateEvent */
  1094. create_event_func = GetProcAddress(wsock2,"WSACreateEvent");
  1095. if(create_event_func == NULL) {
  1096. failf(data,"failed to find WSACreateEvent function (%d)",
  1097. ERRNO);
  1098. FreeLibrary(wsock2);
  1099. return CURLE_FAILED_INIT;
  1100. }
  1101. /* And WSACloseEvent */
  1102. close_event_func = GetProcAddress(wsock2,"WSACloseEvent");
  1103. if(close_event_func == NULL) {
  1104. failf(data,"failed to find WSACloseEvent function (%d)",
  1105. ERRNO);
  1106. FreeLibrary(wsock2);
  1107. return CURLE_FAILED_INIT;
  1108. }
  1109. /* And WSAEventSelect */
  1110. event_select_func = GetProcAddress(wsock2,"WSAEventSelect");
  1111. if(event_select_func == NULL) {
  1112. failf(data,"failed to find WSAEventSelect function (%d)",
  1113. ERRNO);
  1114. FreeLibrary(wsock2);
  1115. return CURLE_FAILED_INIT;
  1116. }
  1117. /* And WSAEnumNetworkEvents */
  1118. enum_netevents_func = GetProcAddress(wsock2,"WSAEnumNetworkEvents");
  1119. if(enum_netevents_func == NULL) {
  1120. failf(data,"failed to find WSAEnumNetworkEvents function (%d)",
  1121. ERRNO);
  1122. FreeLibrary(wsock2);
  1123. return CURLE_FAILED_INIT;
  1124. }
  1125. /* We want to wait for both stdin and the socket. Since
  1126. ** the select() function in winsock only works on sockets
  1127. ** we have to use the WaitForMultipleObjects() call.
  1128. */
  1129. /* First, create a sockets event object */
  1130. event_handle = (WSAEVENT)create_event_func();
  1131. if(event_handle == WSA_INVALID_EVENT) {
  1132. failf(data,"WSACreateEvent failed (%d)", SOCKERRNO);
  1133. FreeLibrary(wsock2);
  1134. return CURLE_FAILED_INIT;
  1135. }
  1136. /* The get the Windows file handle for stdin */
  1137. stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
  1138. /* Create the list of objects to wait for */
  1139. objs[0] = event_handle;
  1140. objs[1] = stdin_handle;
  1141. /* Tell winsock what events we want to listen to */
  1142. if(event_select_func(sockfd, event_handle, FD_READ|FD_CLOSE) == SOCKET_ERROR) {
  1143. close_event_func(event_handle);
  1144. FreeLibrary(wsock2);
  1145. return 0;
  1146. }
  1147. /* If stdin_handle is a pipe, use PeekNamedPipe() method to check it,
  1148. else use the old WaitForMultipleObjects() way */
  1149. if(GetFileType(stdin_handle) == FILE_TYPE_PIPE) {
  1150. /* Don't wait for stdin_handle, just wait for event_handle */
  1151. obj_count = 1;
  1152. /* Check stdin_handle per 100 milliseconds */
  1153. wait_timeout = 100;
  1154. } else {
  1155. obj_count = 2;
  1156. wait_timeout = INFINITE;
  1157. }
  1158. /* Keep on listening and act on events */
  1159. while(keepon) {
  1160. waitret = WaitForMultipleObjects(obj_count, objs, FALSE, wait_timeout);
  1161. switch(waitret) {
  1162. case WAIT_TIMEOUT:
  1163. {
  1164. while(1) {
  1165. if(!PeekNamedPipe(stdin_handle, NULL, 0, NULL, &readfile_read, NULL)) {
  1166. keepon = FALSE;
  1167. code = CURLE_READ_ERROR;
  1168. break;
  1169. }
  1170. if(!readfile_read)
  1171. break;
  1172. if(!ReadFile(stdin_handle, buf, sizeof(data->state.buffer),
  1173. &readfile_read, NULL)) {
  1174. keepon = FALSE;
  1175. code = CURLE_READ_ERROR;
  1176. break;
  1177. }
  1178. code = send_telnet_data(conn, buf, readfile_read);
  1179. if(code) {
  1180. keepon = FALSE;
  1181. break;
  1182. }
  1183. }
  1184. }
  1185. break;
  1186. case WAIT_OBJECT_0 + 1:
  1187. {
  1188. if(!ReadFile(stdin_handle, buf, sizeof(data->state.buffer),
  1189. &readfile_read, NULL)) {
  1190. keepon = FALSE;
  1191. code = CURLE_READ_ERROR;
  1192. break;
  1193. }
  1194. code = send_telnet_data(conn, buf, readfile_read);
  1195. if(code) {
  1196. keepon = FALSE;
  1197. break;
  1198. }
  1199. }
  1200. break;
  1201. case WAIT_OBJECT_0:
  1202. if(enum_netevents_func(sockfd, event_handle, &events)
  1203. != SOCKET_ERROR) {
  1204. if(events.lNetworkEvents & FD_READ) {
  1205. /* This reallu OUGHT to check its return code. */
  1206. (void)Curl_read(conn, sockfd, buf, BUFSIZE - 1, &nread);
  1207. telrcv(conn, (unsigned char *)buf, nread);
  1208. fflush(stdout);
  1209. /* Negotiate if the peer has started negotiating,
  1210. otherwise don't. We don't want to speak telnet with
  1211. non-telnet servers, like POP or SMTP. */
  1212. if(tn->please_negotiate && !tn->already_negotiated) {
  1213. negotiate(conn);
  1214. tn->already_negotiated = 1;
  1215. }
  1216. }
  1217. if(events.lNetworkEvents & FD_CLOSE) {
  1218. keepon = FALSE;
  1219. }
  1220. }
  1221. break;
  1222. }
  1223. }
  1224. /* We called WSACreateEvent, so call WSACloseEvent */
  1225. if(close_event_func(event_handle) == FALSE) {
  1226. infof(data,"WSACloseEvent failed (%d)", SOCKERRNO);
  1227. }
  1228. /* "Forget" pointers into the library we're about to free */
  1229. create_event_func = NULL;
  1230. close_event_func = NULL;
  1231. event_select_func = NULL;
  1232. enum_netevents_func = NULL;
  1233. /* We called LoadLibrary, so call FreeLibrary */
  1234. if(!FreeLibrary(wsock2))
  1235. infof(data,"FreeLibrary(wsock2) failed (%d)", ERRNO);
  1236. #else
  1237. pfd[0].fd = sockfd;
  1238. pfd[0].events = POLLIN;
  1239. pfd[1].fd = 0;
  1240. pfd[1].events = POLLIN;
  1241. interval_ms = 1 * 1000;
  1242. while(keepon) {
  1243. switch (Curl_poll(pfd, 2, interval_ms)) {
  1244. case -1: /* error, stop reading */
  1245. keepon = FALSE;
  1246. continue;
  1247. case 0: /* timeout */
  1248. break;
  1249. default: /* read! */
  1250. if(pfd[1].revents & POLLIN) { /* read from stdin */
  1251. nread = read(0, buf, 255);
  1252. code = send_telnet_data(conn, buf, nread);
  1253. if(code) {
  1254. keepon = FALSE;
  1255. break;
  1256. }
  1257. }
  1258. if(pfd[0].revents & POLLIN) {
  1259. /* This OUGHT to check the return code... */
  1260. (void)Curl_read(conn, sockfd, buf, BUFSIZE - 1, &nread);
  1261. /* if we receive 0 or less here, the server closed the connection and
  1262. we bail out from this! */
  1263. if(nread <= 0) {
  1264. keepon = FALSE;
  1265. break;
  1266. }
  1267. telrcv(conn, (unsigned char *)buf, nread);
  1268. /* Negotiate if the peer has started negotiating,
  1269. otherwise don't. We don't want to speak telnet with
  1270. non-telnet servers, like POP or SMTP. */
  1271. if(tn->please_negotiate && !tn->already_negotiated) {
  1272. negotiate(conn);
  1273. tn->already_negotiated = 1;
  1274. }
  1275. }
  1276. }
  1277. if(data->set.timeout) {
  1278. struct timeval now; /* current time */
  1279. now = Curl_tvnow();
  1280. if(Curl_tvdiff(now, conn->created) >= data->set.timeout) {
  1281. failf(data, "Time-out");
  1282. code = CURLE_OPERATION_TIMEDOUT;
  1283. keepon = FALSE;
  1284. }
  1285. }
  1286. }
  1287. #endif
  1288. /* mark this as "no further transfer wanted" */
  1289. Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
  1290. return code;
  1291. }
  1292. #endif