telnet.c 36 KB

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