telnet.c 45 KB

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