telnet.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681
  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. unsigned short *pval;
  621. if(data->set.verbose) {
  622. if(direction) {
  623. infof(data, "%s IAC SB ", (direction == '<')? "RCVD":"SENT");
  624. if(length >= 3) {
  625. int j;
  626. i = pointer[length-2];
  627. j = pointer[length-1];
  628. if(i != CURL_IAC || j != CURL_SE) {
  629. infof(data, "(terminated by ");
  630. if(CURL_TELOPT_OK(i))
  631. infof(data, "%s ", CURL_TELOPT(i));
  632. else if(CURL_TELCMD_OK(i))
  633. infof(data, "%s ", CURL_TELCMD(i));
  634. else
  635. infof(data, "%u ", i);
  636. if(CURL_TELOPT_OK(j))
  637. infof(data, "%s", CURL_TELOPT(j));
  638. else if(CURL_TELCMD_OK(j))
  639. infof(data, "%s", CURL_TELCMD(j));
  640. else
  641. infof(data, "%d", j);
  642. infof(data, ", not IAC SE!) ");
  643. }
  644. }
  645. length -= 2;
  646. }
  647. if(length < 1) {
  648. infof(data, "(Empty suboption?)");
  649. return;
  650. }
  651. if(CURL_TELOPT_OK(pointer[0])) {
  652. switch(pointer[0]) {
  653. case CURL_TELOPT_TTYPE:
  654. case CURL_TELOPT_XDISPLOC:
  655. case CURL_TELOPT_NEW_ENVIRON:
  656. case CURL_TELOPT_NAWS:
  657. infof(data, "%s", CURL_TELOPT(pointer[0]));
  658. break;
  659. default:
  660. infof(data, "%s (unsupported)", CURL_TELOPT(pointer[0]));
  661. break;
  662. }
  663. }
  664. else
  665. infof(data, "%d (unknown)", pointer[i]);
  666. switch(pointer[0]) {
  667. case CURL_TELOPT_NAWS:
  668. pval = (unsigned short*)(pointer+1);
  669. infof(data, "Width: %hu ; Height: %hu",
  670. ntohs(pval[0]), ntohs(pval[1]));
  671. break;
  672. default:
  673. switch(pointer[1]) {
  674. case CURL_TELQUAL_IS:
  675. infof(data, " IS");
  676. break;
  677. case CURL_TELQUAL_SEND:
  678. infof(data, " SEND");
  679. break;
  680. case CURL_TELQUAL_INFO:
  681. infof(data, " INFO/REPLY");
  682. break;
  683. case CURL_TELQUAL_NAME:
  684. infof(data, " NAME");
  685. break;
  686. }
  687. switch(pointer[0]) {
  688. case CURL_TELOPT_TTYPE:
  689. case CURL_TELOPT_XDISPLOC:
  690. pointer[length] = 0;
  691. infof(data, " \"%s\"", &pointer[2]);
  692. break;
  693. case CURL_TELOPT_NEW_ENVIRON:
  694. if(pointer[1] == CURL_TELQUAL_IS) {
  695. infof(data, " ");
  696. for(i = 3;i < length;i++) {
  697. switch(pointer[i]) {
  698. case CURL_NEW_ENV_VAR:
  699. infof(data, ", ");
  700. break;
  701. case CURL_NEW_ENV_VALUE:
  702. infof(data, " = ");
  703. break;
  704. default:
  705. infof(data, "%c", pointer[i]);
  706. break;
  707. }
  708. }
  709. }
  710. break;
  711. default:
  712. for(i = 2; i < length; i++)
  713. infof(data, " %.2x", pointer[i]);
  714. break;
  715. }
  716. }
  717. if(direction)
  718. infof(data, "\n");
  719. }
  720. }
  721. static CURLcode check_telnet_options(struct connectdata *conn)
  722. {
  723. struct curl_slist *head;
  724. struct curl_slist *beg;
  725. char option_keyword[128] = "";
  726. char option_arg[256] = "";
  727. struct SessionHandle *data = conn->data;
  728. struct TELNET *tn = (struct TELNET *)conn->data->req.protop;
  729. CURLcode result = CURLE_OK;
  730. int binary_option;
  731. /* Add the user name as an environment variable if it
  732. was given on the command line */
  733. if(conn->bits.user_passwd) {
  734. snprintf(option_arg, sizeof(option_arg), "USER,%s", conn->user);
  735. beg = curl_slist_append(tn->telnet_vars, option_arg);
  736. if(!beg) {
  737. curl_slist_free_all(tn->telnet_vars);
  738. tn->telnet_vars = NULL;
  739. return CURLE_OUT_OF_MEMORY;
  740. }
  741. tn->telnet_vars = beg;
  742. tn->us_preferred[CURL_TELOPT_NEW_ENVIRON] = CURL_YES;
  743. }
  744. for(head = data->set.telnet_options; head; head=head->next) {
  745. if(sscanf(head->data, "%127[^= ]%*[ =]%255s",
  746. option_keyword, option_arg) == 2) {
  747. /* Terminal type */
  748. if(Curl_raw_equal(option_keyword, "TTYPE")) {
  749. strncpy(tn->subopt_ttype, option_arg, 31);
  750. tn->subopt_ttype[31] = 0; /* String termination */
  751. tn->us_preferred[CURL_TELOPT_TTYPE] = CURL_YES;
  752. continue;
  753. }
  754. /* Display variable */
  755. if(Curl_raw_equal(option_keyword, "XDISPLOC")) {
  756. strncpy(tn->subopt_xdisploc, option_arg, 127);
  757. tn->subopt_xdisploc[127] = 0; /* String termination */
  758. tn->us_preferred[CURL_TELOPT_XDISPLOC] = CURL_YES;
  759. continue;
  760. }
  761. /* Environment variable */
  762. if(Curl_raw_equal(option_keyword, "NEW_ENV")) {
  763. beg = curl_slist_append(tn->telnet_vars, option_arg);
  764. if(!beg) {
  765. result = CURLE_OUT_OF_MEMORY;
  766. break;
  767. }
  768. tn->telnet_vars = beg;
  769. tn->us_preferred[CURL_TELOPT_NEW_ENVIRON] = CURL_YES;
  770. continue;
  771. }
  772. /* Window Size */
  773. if(Curl_raw_equal(option_keyword, "WS")) {
  774. if(sscanf(option_arg, "%hu%*[xX]%hu",
  775. &tn->subopt_wsx, &tn->subopt_wsy) == 2)
  776. tn->us_preferred[CURL_TELOPT_NAWS] = CURL_YES;
  777. else {
  778. failf(data, "Syntax error in telnet option: %s", head->data);
  779. result = CURLE_TELNET_OPTION_SYNTAX;
  780. break;
  781. }
  782. continue;
  783. }
  784. /* To take care or not of the 8th bit in data exchange */
  785. if(Curl_raw_equal(option_keyword, "BINARY")) {
  786. binary_option=atoi(option_arg);
  787. if(binary_option!=1) {
  788. tn->us_preferred[CURL_TELOPT_BINARY] = CURL_NO;
  789. tn->him_preferred[CURL_TELOPT_BINARY] = CURL_NO;
  790. }
  791. continue;
  792. }
  793. failf(data, "Unknown telnet option %s", head->data);
  794. result = CURLE_UNKNOWN_TELNET_OPTION;
  795. break;
  796. }
  797. else {
  798. failf(data, "Syntax error in telnet option: %s", head->data);
  799. result = CURLE_TELNET_OPTION_SYNTAX;
  800. break;
  801. }
  802. }
  803. if(result) {
  804. curl_slist_free_all(tn->telnet_vars);
  805. tn->telnet_vars = NULL;
  806. }
  807. return result;
  808. }
  809. /*
  810. * suboption()
  811. *
  812. * Look at the sub-option buffer, and try to be helpful to the other
  813. * side.
  814. */
  815. static void suboption(struct connectdata *conn)
  816. {
  817. struct curl_slist *v;
  818. unsigned char temp[2048];
  819. ssize_t bytes_written;
  820. size_t len;
  821. size_t tmplen;
  822. int err;
  823. char varname[128] = "";
  824. char varval[128] = "";
  825. struct SessionHandle *data = conn->data;
  826. struct TELNET *tn = (struct TELNET *)data->req.protop;
  827. printsub(data, '<', (unsigned char *)tn->subbuffer, CURL_SB_LEN(tn)+2);
  828. switch (CURL_SB_GET(tn)) {
  829. case CURL_TELOPT_TTYPE:
  830. len = strlen(tn->subopt_ttype) + 4 + 2;
  831. snprintf((char *)temp, sizeof(temp),
  832. "%c%c%c%c%s%c%c", CURL_IAC, CURL_SB, CURL_TELOPT_TTYPE,
  833. CURL_TELQUAL_IS, tn->subopt_ttype, CURL_IAC, CURL_SE);
  834. bytes_written = swrite(conn->sock[FIRSTSOCKET], temp, len);
  835. if(bytes_written < 0) {
  836. err = SOCKERRNO;
  837. failf(data,"Sending data failed (%d)",err);
  838. }
  839. printsub(data, '>', &temp[2], len-2);
  840. break;
  841. case CURL_TELOPT_XDISPLOC:
  842. len = strlen(tn->subopt_xdisploc) + 4 + 2;
  843. snprintf((char *)temp, sizeof(temp),
  844. "%c%c%c%c%s%c%c", CURL_IAC, CURL_SB, CURL_TELOPT_XDISPLOC,
  845. CURL_TELQUAL_IS, tn->subopt_xdisploc, CURL_IAC, CURL_SE);
  846. bytes_written = swrite(conn->sock[FIRSTSOCKET], temp, len);
  847. if(bytes_written < 0) {
  848. err = SOCKERRNO;
  849. failf(data,"Sending data failed (%d)",err);
  850. }
  851. printsub(data, '>', &temp[2], len-2);
  852. break;
  853. case CURL_TELOPT_NEW_ENVIRON:
  854. snprintf((char *)temp, sizeof(temp),
  855. "%c%c%c%c", CURL_IAC, CURL_SB, CURL_TELOPT_NEW_ENVIRON,
  856. CURL_TELQUAL_IS);
  857. len = 4;
  858. for(v = tn->telnet_vars;v;v = v->next) {
  859. tmplen = (strlen(v->data) + 1);
  860. /* Add the variable only if it fits */
  861. if(len + tmplen < (int)sizeof(temp)-6) {
  862. if(sscanf(v->data, "%127[^,],%127s", varname, varval)) {
  863. snprintf((char *)&temp[len], sizeof(temp) - len,
  864. "%c%s%c%s", CURL_NEW_ENV_VAR, varname,
  865. CURL_NEW_ENV_VALUE, varval);
  866. len += tmplen;
  867. }
  868. }
  869. }
  870. snprintf((char *)&temp[len], sizeof(temp) - len,
  871. "%c%c", CURL_IAC, CURL_SE);
  872. len += 2;
  873. bytes_written = swrite(conn->sock[FIRSTSOCKET], temp, len);
  874. if(bytes_written < 0) {
  875. err = SOCKERRNO;
  876. failf(data,"Sending data failed (%d)",err);
  877. }
  878. printsub(data, '>', &temp[2], len-2);
  879. break;
  880. }
  881. return;
  882. }
  883. /*
  884. * sendsuboption()
  885. *
  886. * Send suboption information to the server side.
  887. */
  888. static void sendsuboption(struct connectdata *conn, int option)
  889. {
  890. ssize_t bytes_written;
  891. int err;
  892. unsigned short x, y;
  893. unsigned char*uc1, *uc2;
  894. struct SessionHandle *data = conn->data;
  895. struct TELNET *tn = (struct TELNET *)data->req.protop;
  896. switch (option) {
  897. case CURL_TELOPT_NAWS:
  898. /* We prepare data to be sent */
  899. CURL_SB_CLEAR(tn);
  900. CURL_SB_ACCUM(tn, CURL_IAC);
  901. CURL_SB_ACCUM(tn, CURL_SB);
  902. CURL_SB_ACCUM(tn, CURL_TELOPT_NAWS);
  903. /* We must deal either with litte or big endien processors */
  904. /* Window size must be sent according to the 'network order' */
  905. x=htons(tn->subopt_wsx);
  906. y=htons(tn->subopt_wsy);
  907. uc1 = (unsigned char*)&x;
  908. uc2 = (unsigned char*)&y;
  909. CURL_SB_ACCUM(tn, uc1[0]);
  910. CURL_SB_ACCUM(tn, uc1[1]);
  911. CURL_SB_ACCUM(tn, uc2[0]);
  912. CURL_SB_ACCUM(tn, uc2[1]);
  913. CURL_SB_ACCUM(tn, CURL_IAC);
  914. CURL_SB_ACCUM(tn, CURL_SE);
  915. CURL_SB_TERM(tn);
  916. /* data suboption is now ready */
  917. printsub(data, '>', (unsigned char *)tn->subbuffer+2,
  918. CURL_SB_LEN(tn)-2);
  919. /* we send the header of the suboption... */
  920. bytes_written = swrite(conn->sock[FIRSTSOCKET], tn->subbuffer, 3);
  921. if(bytes_written < 0) {
  922. err = SOCKERRNO;
  923. failf(data, "Sending data failed (%d)", err);
  924. }
  925. /* ... then the window size with the send_telnet_data() function
  926. to deal with 0xFF cases ... */
  927. send_telnet_data(conn, (char *)tn->subbuffer+3, 4);
  928. /* ... and the footer */
  929. bytes_written = swrite(conn->sock[FIRSTSOCKET], tn->subbuffer+7, 2);
  930. if(bytes_written < 0) {
  931. err = SOCKERRNO;
  932. failf(data, "Sending data failed (%d)", err);
  933. }
  934. break;
  935. }
  936. }
  937. static
  938. CURLcode telrcv(struct connectdata *conn,
  939. const unsigned char *inbuf, /* Data received from socket */
  940. ssize_t count) /* Number of bytes received */
  941. {
  942. unsigned char c;
  943. CURLcode result;
  944. int in = 0;
  945. int startwrite=-1;
  946. struct SessionHandle *data = conn->data;
  947. struct TELNET *tn = (struct TELNET *)data->req.protop;
  948. #define startskipping() \
  949. if(startwrite >= 0) { \
  950. result = Curl_client_write(conn, \
  951. CLIENTWRITE_BODY, \
  952. (char *)&inbuf[startwrite], \
  953. in-startwrite); \
  954. if(result) \
  955. return result; \
  956. } \
  957. startwrite = -1
  958. #define writebyte() \
  959. if(startwrite < 0) \
  960. startwrite = in
  961. #define bufferflush() startskipping()
  962. while(count--) {
  963. c = inbuf[in];
  964. switch (tn->telrcv_state) {
  965. case CURL_TS_CR:
  966. tn->telrcv_state = CURL_TS_DATA;
  967. if(c == '\0') {
  968. startskipping();
  969. break; /* Ignore \0 after CR */
  970. }
  971. writebyte();
  972. break;
  973. case CURL_TS_DATA:
  974. if(c == CURL_IAC) {
  975. tn->telrcv_state = CURL_TS_IAC;
  976. startskipping();
  977. break;
  978. }
  979. else if(c == '\r')
  980. tn->telrcv_state = CURL_TS_CR;
  981. writebyte();
  982. break;
  983. case CURL_TS_IAC:
  984. process_iac:
  985. DEBUGASSERT(startwrite < 0);
  986. switch (c) {
  987. case CURL_WILL:
  988. tn->telrcv_state = CURL_TS_WILL;
  989. break;
  990. case CURL_WONT:
  991. tn->telrcv_state = CURL_TS_WONT;
  992. break;
  993. case CURL_DO:
  994. tn->telrcv_state = CURL_TS_DO;
  995. break;
  996. case CURL_DONT:
  997. tn->telrcv_state = CURL_TS_DONT;
  998. break;
  999. case CURL_SB:
  1000. CURL_SB_CLEAR(tn);
  1001. tn->telrcv_state = CURL_TS_SB;
  1002. break;
  1003. case CURL_IAC:
  1004. tn->telrcv_state = CURL_TS_DATA;
  1005. writebyte();
  1006. break;
  1007. case CURL_DM:
  1008. case CURL_NOP:
  1009. case CURL_GA:
  1010. default:
  1011. tn->telrcv_state = CURL_TS_DATA;
  1012. printoption(data, "RCVD", CURL_IAC, c);
  1013. break;
  1014. }
  1015. break;
  1016. case CURL_TS_WILL:
  1017. printoption(data, "RCVD", CURL_WILL, c);
  1018. tn->please_negotiate = 1;
  1019. rec_will(conn, c);
  1020. tn->telrcv_state = CURL_TS_DATA;
  1021. break;
  1022. case CURL_TS_WONT:
  1023. printoption(data, "RCVD", CURL_WONT, c);
  1024. tn->please_negotiate = 1;
  1025. rec_wont(conn, c);
  1026. tn->telrcv_state = CURL_TS_DATA;
  1027. break;
  1028. case CURL_TS_DO:
  1029. printoption(data, "RCVD", CURL_DO, c);
  1030. tn->please_negotiate = 1;
  1031. rec_do(conn, c);
  1032. tn->telrcv_state = CURL_TS_DATA;
  1033. break;
  1034. case CURL_TS_DONT:
  1035. printoption(data, "RCVD", CURL_DONT, c);
  1036. tn->please_negotiate = 1;
  1037. rec_dont(conn, c);
  1038. tn->telrcv_state = CURL_TS_DATA;
  1039. break;
  1040. case CURL_TS_SB:
  1041. if(c == CURL_IAC)
  1042. tn->telrcv_state = CURL_TS_SE;
  1043. else
  1044. CURL_SB_ACCUM(tn,c);
  1045. break;
  1046. case CURL_TS_SE:
  1047. if(c != CURL_SE) {
  1048. if(c != CURL_IAC) {
  1049. /*
  1050. * This is an error. We only expect to get "IAC IAC" or "IAC SE".
  1051. * Several things may have happened. An IAC was not doubled, the
  1052. * IAC SE was left off, or another option got inserted into the
  1053. * suboption are all possibilities. If we assume that the IAC was
  1054. * not doubled, and really the IAC SE was left off, we could get
  1055. * into an infinate loop here. So, instead, we terminate the
  1056. * suboption, and process the partial suboption if we can.
  1057. */
  1058. CURL_SB_ACCUM(tn, CURL_IAC);
  1059. CURL_SB_ACCUM(tn, c);
  1060. tn->subpointer -= 2;
  1061. CURL_SB_TERM(tn);
  1062. printoption(data, "In SUBOPTION processing, RCVD", CURL_IAC, c);
  1063. suboption(conn); /* handle sub-option */
  1064. tn->telrcv_state = CURL_TS_IAC;
  1065. goto process_iac;
  1066. }
  1067. CURL_SB_ACCUM(tn,c);
  1068. tn->telrcv_state = CURL_TS_SB;
  1069. }
  1070. else
  1071. {
  1072. CURL_SB_ACCUM(tn, CURL_IAC);
  1073. CURL_SB_ACCUM(tn, CURL_SE);
  1074. tn->subpointer -= 2;
  1075. CURL_SB_TERM(tn);
  1076. suboption(conn); /* handle sub-option */
  1077. tn->telrcv_state = CURL_TS_DATA;
  1078. }
  1079. break;
  1080. }
  1081. ++in;
  1082. }
  1083. bufferflush();
  1084. return CURLE_OK;
  1085. }
  1086. /* Escape and send a telnet data block */
  1087. /* TODO: write large chunks of data instead of one byte at a time */
  1088. static CURLcode send_telnet_data(struct connectdata *conn,
  1089. char *buffer, ssize_t nread)
  1090. {
  1091. unsigned char outbuf[2];
  1092. ssize_t bytes_written, total_written;
  1093. int out_count;
  1094. CURLcode result = CURLE_OK;
  1095. while(!result && nread--) {
  1096. outbuf[0] = *buffer++;
  1097. out_count = 1;
  1098. if(outbuf[0] == CURL_IAC)
  1099. outbuf[out_count++] = CURL_IAC;
  1100. total_written = 0;
  1101. do {
  1102. /* Make sure socket is writable to avoid EWOULDBLOCK condition */
  1103. struct pollfd pfd[1];
  1104. pfd[0].fd = conn->sock[FIRSTSOCKET];
  1105. pfd[0].events = POLLOUT;
  1106. switch (Curl_poll(pfd, 1, -1)) {
  1107. case -1: /* error, abort writing */
  1108. case 0: /* timeout (will never happen) */
  1109. result = CURLE_SEND_ERROR;
  1110. break;
  1111. default: /* write! */
  1112. bytes_written = 0;
  1113. result = Curl_write(conn, conn->sock[FIRSTSOCKET],
  1114. outbuf+total_written, out_count-total_written,
  1115. &bytes_written);
  1116. total_written += bytes_written;
  1117. break;
  1118. }
  1119. /* handle partial write */
  1120. } while(!result && total_written < out_count);
  1121. }
  1122. return result;
  1123. }
  1124. static CURLcode telnet_done(struct connectdata *conn,
  1125. CURLcode status, bool premature)
  1126. {
  1127. struct TELNET *tn = (struct TELNET *)conn->data->req.protop;
  1128. (void)status; /* unused */
  1129. (void)premature; /* not used */
  1130. if(!tn)
  1131. return CURLE_OK;
  1132. curl_slist_free_all(tn->telnet_vars);
  1133. tn->telnet_vars = NULL;
  1134. Curl_safefree(conn->data->req.protop);
  1135. return CURLE_OK;
  1136. }
  1137. static CURLcode telnet_do(struct connectdata *conn, bool *done)
  1138. {
  1139. CURLcode code;
  1140. struct SessionHandle *data = conn->data;
  1141. curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
  1142. #ifdef USE_WINSOCK
  1143. HMODULE wsock2;
  1144. WSOCK2_FUNC close_event_func;
  1145. WSOCK2_FUNC create_event_func;
  1146. WSOCK2_FUNC event_select_func;
  1147. WSOCK2_FUNC enum_netevents_func;
  1148. WSAEVENT event_handle;
  1149. WSANETWORKEVENTS events;
  1150. HANDLE stdin_handle;
  1151. HANDLE objs[2];
  1152. DWORD obj_count;
  1153. DWORD wait_timeout;
  1154. DWORD waitret;
  1155. DWORD readfile_read;
  1156. int err;
  1157. #else
  1158. int interval_ms;
  1159. struct pollfd pfd[2];
  1160. int poll_cnt;
  1161. curl_off_t total_dl = 0;
  1162. curl_off_t total_ul = 0;
  1163. #endif
  1164. ssize_t nread;
  1165. struct timeval now;
  1166. bool keepon = TRUE;
  1167. char *buf = data->state.buffer;
  1168. struct TELNET *tn;
  1169. *done = TRUE; /* unconditionally */
  1170. code = init_telnet(conn);
  1171. if(code)
  1172. return code;
  1173. tn = (struct TELNET *)data->req.protop;
  1174. code = check_telnet_options(conn);
  1175. if(code)
  1176. return code;
  1177. #ifdef USE_WINSOCK
  1178. /*
  1179. ** This functionality only works with WinSock >= 2.0. So,
  1180. ** make sure have it.
  1181. */
  1182. code = check_wsock2(data);
  1183. if(code)
  1184. return code;
  1185. /* OK, so we have WinSock 2.0. We need to dynamically */
  1186. /* load ws2_32.dll and get the function pointers we need. */
  1187. wsock2 = LoadLibrary(TEXT("WS2_32.DLL"));
  1188. if(wsock2 == NULL) {
  1189. failf(data,"failed to load WS2_32.DLL (%d)", ERRNO);
  1190. return CURLE_FAILED_INIT;
  1191. }
  1192. /* Grab a pointer to WSACreateEvent */
  1193. create_event_func = GetProcAddress(wsock2,"WSACreateEvent");
  1194. if(create_event_func == NULL) {
  1195. failf(data,"failed to find WSACreateEvent function (%d)",
  1196. ERRNO);
  1197. FreeLibrary(wsock2);
  1198. return CURLE_FAILED_INIT;
  1199. }
  1200. /* And WSACloseEvent */
  1201. close_event_func = GetProcAddress(wsock2,"WSACloseEvent");
  1202. if(close_event_func == NULL) {
  1203. failf(data,"failed to find WSACloseEvent function (%d)",
  1204. ERRNO);
  1205. FreeLibrary(wsock2);
  1206. return CURLE_FAILED_INIT;
  1207. }
  1208. /* And WSAEventSelect */
  1209. event_select_func = GetProcAddress(wsock2,"WSAEventSelect");
  1210. if(event_select_func == NULL) {
  1211. failf(data,"failed to find WSAEventSelect function (%d)",
  1212. ERRNO);
  1213. FreeLibrary(wsock2);
  1214. return CURLE_FAILED_INIT;
  1215. }
  1216. /* And WSAEnumNetworkEvents */
  1217. enum_netevents_func = GetProcAddress(wsock2,"WSAEnumNetworkEvents");
  1218. if(enum_netevents_func == NULL) {
  1219. failf(data,"failed to find WSAEnumNetworkEvents function (%d)",
  1220. ERRNO);
  1221. FreeLibrary(wsock2);
  1222. return CURLE_FAILED_INIT;
  1223. }
  1224. /* We want to wait for both stdin and the socket. Since
  1225. ** the select() function in winsock only works on sockets
  1226. ** we have to use the WaitForMultipleObjects() call.
  1227. */
  1228. /* First, create a sockets event object */
  1229. event_handle = (WSAEVENT)create_event_func();
  1230. if(event_handle == WSA_INVALID_EVENT) {
  1231. failf(data,"WSACreateEvent failed (%d)", SOCKERRNO);
  1232. FreeLibrary(wsock2);
  1233. return CURLE_FAILED_INIT;
  1234. }
  1235. /* Tell winsock what events we want to listen to */
  1236. if(event_select_func(sockfd, event_handle, FD_READ|FD_CLOSE) ==
  1237. SOCKET_ERROR) {
  1238. close_event_func(event_handle);
  1239. FreeLibrary(wsock2);
  1240. return CURLE_OK;
  1241. }
  1242. /* The get the Windows file handle for stdin */
  1243. stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
  1244. /* Create the list of objects to wait for */
  1245. objs[0] = event_handle;
  1246. objs[1] = stdin_handle;
  1247. /* If stdin_handle is a pipe, use PeekNamedPipe() method to check it,
  1248. else use the old WaitForMultipleObjects() way */
  1249. if(GetFileType(stdin_handle) == FILE_TYPE_PIPE ||
  1250. data->set.is_fread_set) {
  1251. /* Don't wait for stdin_handle, just wait for event_handle */
  1252. obj_count = 1;
  1253. /* Check stdin_handle per 100 milliseconds */
  1254. wait_timeout = 100;
  1255. }
  1256. else {
  1257. obj_count = 2;
  1258. wait_timeout = 1000;
  1259. }
  1260. /* Keep on listening and act on events */
  1261. while(keepon) {
  1262. waitret = WaitForMultipleObjects(obj_count, objs, FALSE, wait_timeout);
  1263. switch(waitret) {
  1264. case WAIT_TIMEOUT:
  1265. {
  1266. for(;;) {
  1267. if(obj_count == 1) {
  1268. /* read from user-supplied method */
  1269. code = (int)conn->fread_func(buf, 1, BUFSIZE - 1, conn->fread_in);
  1270. if(code == CURL_READFUNC_ABORT) {
  1271. keepon = FALSE;
  1272. code = CURLE_READ_ERROR;
  1273. break;
  1274. }
  1275. if(code == CURL_READFUNC_PAUSE)
  1276. break;
  1277. if(code == 0) /* no bytes */
  1278. break;
  1279. readfile_read = code; /* fall thru with number of bytes read */
  1280. }
  1281. else {
  1282. /* read from stdin */
  1283. if(!PeekNamedPipe(stdin_handle, NULL, 0, NULL,
  1284. &readfile_read, NULL)) {
  1285. keepon = FALSE;
  1286. code = CURLE_READ_ERROR;
  1287. break;
  1288. }
  1289. if(!readfile_read)
  1290. break;
  1291. if(!ReadFile(stdin_handle, buf, sizeof(data->state.buffer),
  1292. &readfile_read, NULL)) {
  1293. keepon = FALSE;
  1294. code = CURLE_READ_ERROR;
  1295. break;
  1296. }
  1297. }
  1298. code = send_telnet_data(conn, buf, readfile_read);
  1299. if(code) {
  1300. keepon = FALSE;
  1301. break;
  1302. }
  1303. }
  1304. }
  1305. break;
  1306. case WAIT_OBJECT_0 + 1:
  1307. {
  1308. if(!ReadFile(stdin_handle, buf, sizeof(data->state.buffer),
  1309. &readfile_read, NULL)) {
  1310. keepon = FALSE;
  1311. code = CURLE_READ_ERROR;
  1312. break;
  1313. }
  1314. code = send_telnet_data(conn, buf, readfile_read);
  1315. if(code) {
  1316. keepon = FALSE;
  1317. break;
  1318. }
  1319. }
  1320. break;
  1321. case WAIT_OBJECT_0:
  1322. events.lNetworkEvents = 0;
  1323. if(SOCKET_ERROR == enum_netevents_func(sockfd, event_handle, &events)) {
  1324. if((err = SOCKERRNO) != EINPROGRESS) {
  1325. infof(data,"WSAEnumNetworkEvents failed (%d)", err);
  1326. keepon = FALSE;
  1327. code = CURLE_READ_ERROR;
  1328. }
  1329. break;
  1330. }
  1331. if(events.lNetworkEvents & FD_READ) {
  1332. /* read data from network */
  1333. code = Curl_read(conn, sockfd, buf, BUFSIZE - 1, &nread);
  1334. /* read would've blocked. Loop again */
  1335. if(code == CURLE_AGAIN)
  1336. break;
  1337. /* returned not-zero, this an error */
  1338. else if(code) {
  1339. keepon = FALSE;
  1340. break;
  1341. }
  1342. /* returned zero but actually received 0 or less here,
  1343. the server closed the connection and we bail out */
  1344. else if(nread <= 0) {
  1345. keepon = FALSE;
  1346. break;
  1347. }
  1348. code = telrcv(conn, (unsigned char *)buf, nread);
  1349. if(code) {
  1350. keepon = FALSE;
  1351. break;
  1352. }
  1353. /* Negotiate if the peer has started negotiating,
  1354. otherwise don't. We don't want to speak telnet with
  1355. non-telnet servers, like POP or SMTP. */
  1356. if(tn->please_negotiate && !tn->already_negotiated) {
  1357. negotiate(conn);
  1358. tn->already_negotiated = 1;
  1359. }
  1360. }
  1361. if(events.lNetworkEvents & FD_CLOSE) {
  1362. keepon = FALSE;
  1363. }
  1364. break;
  1365. }
  1366. if(data->set.timeout) {
  1367. now = Curl_tvnow();
  1368. if(Curl_tvdiff(now, conn->created) >= data->set.timeout) {
  1369. failf(data, "Time-out");
  1370. code = CURLE_OPERATION_TIMEDOUT;
  1371. keepon = FALSE;
  1372. }
  1373. }
  1374. }
  1375. /* We called WSACreateEvent, so call WSACloseEvent */
  1376. if(!close_event_func(event_handle)) {
  1377. infof(data,"WSACloseEvent failed (%d)", SOCKERRNO);
  1378. }
  1379. /* "Forget" pointers into the library we're about to free */
  1380. create_event_func = NULL;
  1381. close_event_func = NULL;
  1382. event_select_func = NULL;
  1383. enum_netevents_func = NULL;
  1384. /* We called LoadLibrary, so call FreeLibrary */
  1385. if(!FreeLibrary(wsock2))
  1386. infof(data,"FreeLibrary(wsock2) failed (%d)", ERRNO);
  1387. #else
  1388. pfd[0].fd = sockfd;
  1389. pfd[0].events = POLLIN;
  1390. if(conn->fread_func != (curl_read_callback)fread) {
  1391. poll_cnt = 1;
  1392. interval_ms = 100; /* poll user-supplied read function */
  1393. }
  1394. else {
  1395. /* really using fread, so infile is a FILE* */
  1396. pfd[1].fd = fileno((FILE *)conn->fread_in);
  1397. pfd[1].events = POLLIN;
  1398. poll_cnt = 2;
  1399. interval_ms = 1 * 1000;
  1400. }
  1401. while(keepon) {
  1402. switch (Curl_poll(pfd, poll_cnt, interval_ms)) {
  1403. case -1: /* error, stop reading */
  1404. keepon = FALSE;
  1405. continue;
  1406. case 0: /* timeout */
  1407. pfd[0].revents = 0;
  1408. pfd[1].revents = 0;
  1409. /* fall through */
  1410. default: /* read! */
  1411. if(pfd[0].revents & POLLIN) {
  1412. /* read data from network */
  1413. code = Curl_read(conn, sockfd, buf, BUFSIZE - 1, &nread);
  1414. /* read would've blocked. Loop again */
  1415. if(code == CURLE_AGAIN)
  1416. break;
  1417. /* returned not-zero, this an error */
  1418. else if(code) {
  1419. keepon = FALSE;
  1420. break;
  1421. }
  1422. /* returned zero but actually received 0 or less here,
  1423. the server closed the connection and we bail out */
  1424. else if(nread <= 0) {
  1425. keepon = FALSE;
  1426. break;
  1427. }
  1428. total_dl += nread;
  1429. Curl_pgrsSetDownloadCounter(data, total_dl);
  1430. code = telrcv(conn, (unsigned char *)buf, nread);
  1431. if(code) {
  1432. keepon = FALSE;
  1433. break;
  1434. }
  1435. /* Negotiate if the peer has started negotiating,
  1436. otherwise don't. We don't want to speak telnet with
  1437. non-telnet servers, like POP or SMTP. */
  1438. if(tn->please_negotiate && !tn->already_negotiated) {
  1439. negotiate(conn);
  1440. tn->already_negotiated = 1;
  1441. }
  1442. }
  1443. nread = 0;
  1444. if(poll_cnt == 2) {
  1445. if(pfd[1].revents & POLLIN) { /* read from in file */
  1446. nread = read(pfd[1].fd, buf, BUFSIZE - 1);
  1447. }
  1448. }
  1449. else {
  1450. /* read from user-supplied method */
  1451. nread = (int)conn->fread_func(buf, 1, BUFSIZE - 1, conn->fread_in);
  1452. if(nread == CURL_READFUNC_ABORT) {
  1453. keepon = FALSE;
  1454. break;
  1455. }
  1456. if(nread == CURL_READFUNC_PAUSE)
  1457. break;
  1458. }
  1459. if(nread > 0) {
  1460. code = send_telnet_data(conn, buf, nread);
  1461. if(code) {
  1462. keepon = FALSE;
  1463. break;
  1464. }
  1465. total_ul += nread;
  1466. Curl_pgrsSetUploadCounter(data, total_ul);
  1467. }
  1468. else if(nread < 0)
  1469. keepon = FALSE;
  1470. break;
  1471. } /* poll switch statement */
  1472. if(data->set.timeout) {
  1473. now = Curl_tvnow();
  1474. if(Curl_tvdiff(now, conn->created) >= data->set.timeout) {
  1475. failf(data, "Time-out");
  1476. code = CURLE_OPERATION_TIMEDOUT;
  1477. keepon = FALSE;
  1478. }
  1479. }
  1480. if(Curl_pgrsUpdate(conn)) {
  1481. code = CURLE_ABORTED_BY_CALLBACK;
  1482. break;
  1483. }
  1484. }
  1485. #endif
  1486. /* mark this as "no further transfer wanted" */
  1487. Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
  1488. return code;
  1489. }
  1490. #endif