telnet.c 45 KB

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