telnet.c 45 KB

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