telnet.c 46 KB

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