telnet.c 45 KB

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