telnet.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585
  1. /*++
  2. Copyright (c) 2015 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. telnet.c
  5. Abstract:
  6. This module implements a very simple telnet client.
  7. Author:
  8. Evan Green 4-Oct-2015
  9. Environment:
  10. POSIX
  11. --*/
  12. //
  13. // ------------------------------------------------------------------- Includes
  14. //
  15. //
  16. // Add some definitions to get the strings out of telnet.h. The telnet daemon
  17. // also relies on the client having the string declarations here.
  18. //
  19. #define TELCMDS 1
  20. #include <minoca/lib/types.h>
  21. #include <arpa/telnet.h>
  22. #include <assert.h>
  23. #include <errno.h>
  24. #include <getopt.h>
  25. #include <netdb.h>
  26. #include <poll.h>
  27. #include <signal.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <sys/socket.h>
  31. #include <unistd.h>
  32. #include "swlib.h"
  33. //
  34. // --------------------------------------------------------------------- Macros
  35. //
  36. //
  37. // ---------------------------------------------------------------- Definitions
  38. //
  39. #define TELNET_VERSION_MAJOR 1
  40. #define TELNET_VERSION_MINOR 0
  41. #define TELNET_USAGE \
  42. "usage: telnet [-l user] host [port]\n" \
  43. "The telnet utility implements a simple telnet client. Options are:\n" \
  44. " --help -- Show this help text and exit.\n" \
  45. " --version -- Print the application version information and exit.\n"
  46. #define TELNET_OPTIONS_STRING "l:hv"
  47. #define TELNET_ENTERING_MODE_FORMAT \
  48. "\nEntering %s mode. Escape character is %s.\n"
  49. #define TELNET_BUFFER_SIZE 256
  50. #define TELNET_IAC_BUFFER_SIZE 64
  51. //
  52. // Define the escape character, ^].
  53. //
  54. #define TELNET_ESCAPE 0x1D
  55. #define TELNET_FLAG_ECHO 0x00000001
  56. #define TELNET_FLAG_SUPPRESS_GO_AHEAD 0x00000002
  57. //
  58. // ------------------------------------------------------ Data Type Definitions
  59. //
  60. typedef enum _TELNET_STATE {
  61. TelnetStateInvalid,
  62. TelnetStateNormal,
  63. TelnetStateCopy,
  64. TelnetStateIac,
  65. TelnetStateOption,
  66. TelnetStateSubnegotiation1,
  67. TelnetStateSubnegotiation2,
  68. TelnetStateCr
  69. } TELNET_STATE, *PTELNET_STATE;
  70. typedef enum _TELNET_CHARACTER_MODE {
  71. TelnetCharacterModeTry,
  72. TelnetCharacterModeOn,
  73. TelnetCharacterModeOff
  74. } TELNET_CHARACTER_MODE, *PTELNET_CHARACTER_MODE;
  75. /*++
  76. Structure Description:
  77. This structure defines the context for an instantiation of the telnet
  78. client.
  79. Members:
  80. WindowHeight - Stores the window height in characters.
  81. WindowWidth - Stores the window width in characters.
  82. TerminalType - Stores the terminal type.
  83. Socket - Stores a pointer to the socket.
  84. Poll - Stores the poll descriptors.
  85. Sigint - Stores a boolean indicating if a pending sigint occurred.
  86. Exit - Stores a boolean indicating an exit is requested.
  87. Buffer - Stores the data buffer.
  88. State - Stores the current telnet command state.
  89. Wish - Stores the option that the remote side is asking/telling this client
  90. about.
  91. Flags - Stores telnet flags. See TELNET_FLAG_* definitions.
  92. CharacterMode - Stores the character mode negotiation state.
  93. IacBuffer - Stores the buffer used to send telnet options and
  94. subnegotiations.
  95. IacSize - Stores the size of the IAC buffer.
  96. --*/
  97. typedef struct _TELNET_CONTEXT {
  98. int WindowHeight;
  99. int WindowWidth;
  100. PSTR TerminalType;
  101. int Socket;
  102. struct pollfd Poll[2];
  103. BOOL Sigint;
  104. BOOL Exit;
  105. UCHAR Buffer[TELNET_BUFFER_SIZE];
  106. TELNET_STATE State;
  107. UCHAR Wish;
  108. ULONG Flags;
  109. TELNET_CHARACTER_MODE CharacterMode;
  110. UCHAR IacBuffer[TELNET_IAC_BUFFER_SIZE];
  111. ULONG IacSize;
  112. } TELNET_CONTEXT, *PTELNET_CONTEXT;
  113. //
  114. // ----------------------------------------------- Internal Function Prototypes
  115. //
  116. void
  117. TelnetSigintHandler (
  118. int Signal
  119. );
  120. INT
  121. TelnetWriteToSocket (
  122. PTELNET_CONTEXT Context,
  123. size_t Size
  124. );
  125. INT
  126. TelnetWriteToOutput (
  127. PTELNET_CONTEXT Context,
  128. size_t Size
  129. );
  130. INT
  131. TelnetWrite (
  132. int Descriptor,
  133. void *Buffer,
  134. size_t Size
  135. );
  136. INT
  137. TelnetEscape (
  138. PTELNET_CONTEXT Context
  139. );
  140. VOID
  141. TelnetHandleOption (
  142. PTELNET_CONTEXT Context,
  143. CHAR Option
  144. );
  145. VOID
  146. TelnetHandleSubnegotiation (
  147. PTELNET_CONTEXT Context,
  148. CHAR Option
  149. );
  150. VOID
  151. TelnetHandleEchoOption (
  152. PTELNET_CONTEXT Context
  153. );
  154. VOID
  155. TelnetHandleSgaOption (
  156. PTELNET_CONTEXT Context
  157. );
  158. VOID
  159. TelnetHandleTTypeOption (
  160. PTELNET_CONTEXT Context
  161. );
  162. VOID
  163. TelnetHandleNawsOption (
  164. PTELNET_CONTEXT Context
  165. );
  166. VOID
  167. TelnetHandleUnsupportedOption (
  168. PTELNET_CONTEXT Context,
  169. CHAR Option
  170. );
  171. VOID
  172. TelnetSendWindowSize (
  173. PTELNET_CONTEXT Context,
  174. UCHAR Option,
  175. INT Width,
  176. INT Height
  177. );
  178. VOID
  179. TelnetSendSuboptIac (
  180. PTELNET_CONTEXT Context,
  181. UCHAR Option,
  182. PSTR String
  183. );
  184. VOID
  185. TelnetSendDoLineMode (
  186. PTELNET_CONTEXT Context
  187. );
  188. VOID
  189. TelnetSendWillCharMode (
  190. PTELNET_CONTEXT Context
  191. );
  192. VOID
  193. TelnetSetConsoleMode (
  194. PTELNET_CONTEXT Context
  195. );
  196. VOID
  197. TelnetAddIacWish (
  198. PTELNET_CONTEXT Context,
  199. UCHAR Wish,
  200. UCHAR Option
  201. );
  202. VOID
  203. TelnetAddIac (
  204. PTELNET_CONTEXT Context,
  205. UCHAR Character
  206. );
  207. VOID
  208. TelnetFlushIacs (
  209. PTELNET_CONTEXT Context
  210. );
  211. //
  212. // -------------------------------------------------------------------- Globals
  213. //
  214. struct option TelnetLongOptions[] = {
  215. {"help", no_argument, 0, 'h'},
  216. {"version", no_argument, 0, 'V'},
  217. {NULL, 0, 0, 0},
  218. };
  219. PTELNET_CONTEXT TelnetGlobalContext;
  220. //
  221. // ------------------------------------------------------------------ Functions
  222. //
  223. INT
  224. TelnetMain (
  225. INT ArgumentCount,
  226. CHAR **Arguments
  227. )
  228. /*++
  229. Routine Description:
  230. This routine is the main entry point for the telnet utility.
  231. Arguments:
  232. ArgumentCount - Supplies the number of command line arguments the program
  233. was invoked with.
  234. Arguments - Supplies a tokenized array of command line arguments.
  235. Return Value:
  236. Returns an integer exit code. 0 for success, nonzero otherwise.
  237. --*/
  238. {
  239. struct sigaction Action;
  240. struct addrinfo *Address;
  241. struct addrinfo *AddressInfo;
  242. ULONG ArgumentIndex;
  243. ssize_t BytesDone;
  244. TELNET_CONTEXT Context;
  245. PSTR Host;
  246. INT Option;
  247. struct sigaction OriginalAction;
  248. PSTR PortString;
  249. int Status;
  250. int Value;
  251. AddressInfo = NULL;
  252. memset(&Context, 0, sizeof(TELNET_CONTEXT));
  253. Context.State = TelnetStateNormal;
  254. Context.Socket = -1;
  255. Context.TerminalType = getenv("TERM");
  256. SwGetTerminalDimensions(&(Context.WindowWidth), &(Context.WindowHeight));
  257. SwSetRawInputMode(NULL, NULL);
  258. TelnetGlobalContext = &Context;
  259. memset(&Action, 0, sizeof(struct sigaction));
  260. Action.sa_handler = TelnetSigintHandler;
  261. sigaction(SIGINT, &Action, &OriginalAction);
  262. //
  263. // Process the control arguments.
  264. //
  265. while (TRUE) {
  266. Option = getopt_long(ArgumentCount,
  267. Arguments,
  268. TELNET_OPTIONS_STRING,
  269. TelnetLongOptions,
  270. NULL);
  271. if (Option == -1) {
  272. break;
  273. }
  274. if ((Option == '?') || (Option == ':')) {
  275. Status = 1;
  276. goto MainEnd;
  277. }
  278. switch (Option) {
  279. case 'V':
  280. SwPrintVersion(TELNET_VERSION_MAJOR, TELNET_VERSION_MINOR);
  281. return 1;
  282. case 'h':
  283. printf(TELNET_USAGE);
  284. return 1;
  285. default:
  286. assert(FALSE);
  287. Status = 1;
  288. goto MainEnd;
  289. }
  290. }
  291. ArgumentIndex = optind;
  292. if (ArgumentIndex >= ArgumentCount) {
  293. SwPrintError(0, NULL, "Argument required. Try --help for usage");
  294. Status = 1;
  295. goto MainEnd;
  296. }
  297. PortString = "23";
  298. Host = Arguments[ArgumentIndex];
  299. ArgumentIndex += 1;
  300. if (ArgumentIndex < ArgumentCount) {
  301. PortString = Arguments[ArgumentIndex];
  302. ArgumentIndex += 1;
  303. if (ArgumentIndex < ArgumentCount) {
  304. SwPrintError(0, NULL, "Too many arguments");
  305. Status = 1;
  306. goto MainEnd;
  307. }
  308. }
  309. Status = getaddrinfo(Host, PortString, NULL, &AddressInfo);
  310. if (Status != 0) {
  311. SwPrintError(0,
  312. NULL,
  313. "Cannot resolve %s:%s: %s.\n",
  314. Host,
  315. PortString,
  316. gai_strerror(Status));
  317. Status = errno;
  318. if (Status == 0) {
  319. Status = 1;
  320. }
  321. goto MainEnd;
  322. }
  323. Address = AddressInfo;
  324. while (Address != NULL) {
  325. if (Address->ai_socktype == SOCK_STREAM) {
  326. Context.Socket = socket(Address->ai_family,
  327. Address->ai_socktype,
  328. Address->ai_protocol);
  329. if (Context.Socket < 0) {
  330. Status = errno;
  331. SwPrintError(Status, NULL, "Unable to create socket");
  332. goto MainEnd;
  333. }
  334. Status = connect(Context.Socket,
  335. Address->ai_addr,
  336. Address->ai_addrlen);
  337. if (Status != 0) {
  338. Status = errno;
  339. SwPrintError(Status, Host, "Unable to connect");
  340. close(Context.Socket);
  341. Context.Socket = -1;
  342. }
  343. break;
  344. }
  345. Address = Address->ai_next;
  346. }
  347. if (Context.Socket < 0) {
  348. Status = 1;
  349. SwPrintError(0, Host, "Connection failed");
  350. goto MainEnd;
  351. }
  352. Value = 1;
  353. setsockopt(Context.Socket, SOL_SOCKET, SO_KEEPALIVE, &Value, sizeof(Value));
  354. Context.Poll[0].fd = STDIN_FILENO;
  355. Context.Poll[0].events = POLLIN;
  356. Context.Poll[1].fd = Context.Socket;
  357. Context.Poll[1].events = POLLIN;
  358. //
  359. // This is the main loop, which shuttles data.
  360. //
  361. while (Context.Exit == FALSE) {
  362. if (poll(Context.Poll, 2, -1) < 0) {
  363. if (Context.Sigint != 0) {
  364. TelnetEscape(&Context);
  365. } else {
  366. if (errno != 0) {
  367. Status = errno;
  368. if (errno == EINTR) {
  369. continue;
  370. }
  371. SwPrintError(Status, NULL, "Error");
  372. break;
  373. }
  374. }
  375. }
  376. //
  377. // Check standard in heading out to the socket.
  378. //
  379. if ((Context.Poll[0].revents & POLLIN) != 0) {
  380. do {
  381. BytesDone = read(STDIN_FILENO,
  382. Context.Buffer,
  383. TELNET_BUFFER_SIZE);
  384. } while ((BytesDone <= 0) && (errno == EINTR));
  385. if (BytesDone == 0) {
  386. break;
  387. } else if (BytesDone < 0) {
  388. Status = errno;
  389. SwPrintError(Status, NULL, "Failed to read");
  390. goto MainEnd;
  391. }
  392. TelnetWriteToSocket(&Context, BytesDone);
  393. }
  394. //
  395. // Check the socket headed off to standard out.
  396. //
  397. if ((Context.Poll[1].revents & POLLIN) != 0) {
  398. do {
  399. BytesDone = read(Context.Socket,
  400. Context.Buffer,
  401. TELNET_BUFFER_SIZE);
  402. } while ((BytesDone <= 0) && (errno == EINTR));
  403. if (BytesDone == 0) {
  404. break;
  405. } else if (BytesDone < 0) {
  406. Status = errno;
  407. SwPrintError(Status, NULL, "Failed to read");
  408. goto MainEnd;
  409. }
  410. TelnetWriteToOutput(&Context, BytesDone);
  411. }
  412. }
  413. shutdown(Context.Socket, SHUT_RDWR);
  414. MainEnd:
  415. sigaction(SIGINT, &OriginalAction, NULL);
  416. TelnetGlobalContext = NULL;
  417. if (Context.Socket >= 0) {
  418. close(Context.Socket);
  419. }
  420. if (AddressInfo != NULL) {
  421. freeaddrinfo(AddressInfo);
  422. }
  423. SwRestoreInputMode();
  424. return Status;
  425. }
  426. //
  427. // --------------------------------------------------------- Internal Functions
  428. //
  429. void
  430. TelnetSigintHandler (
  431. int Signal
  432. )
  433. /*++
  434. Routine Description:
  435. This routine handles SIGINT signals, simply marking that they occurred.
  436. Arguments:
  437. Signal - Supplies the signal number that fired, which in this case is
  438. SIGINT.
  439. Return Value:
  440. None.
  441. --*/
  442. {
  443. TelnetGlobalContext->Sigint = TRUE;
  444. return;
  445. }
  446. INT
  447. TelnetWriteToSocket (
  448. PTELNET_CONTEXT Context,
  449. size_t Size
  450. )
  451. /*++
  452. Routine Description:
  453. This routine writes data from standard input out to the socket,
  454. transforming to work with telnet as needed.
  455. Arguments:
  456. Context - Supplies a pointer to the application context.
  457. Size - Supplies the number of valid bytes in the buffer.
  458. Return Value:
  459. Returns an integer exit code. 0 for success, nonzero otherwise.
  460. --*/
  461. {
  462. UCHAR Character;
  463. UINTN Index;
  464. PUCHAR Input;
  465. UCHAR Output[TELNET_BUFFER_SIZE * 2];
  466. size_t OutSize;
  467. INT Status;
  468. Input = Context->Buffer;
  469. OutSize = 0;
  470. for (Index = 0; Index < Size; Index += 1) {
  471. Character = *Input;
  472. if (Character == TELNET_ESCAPE) {
  473. Status = TelnetEscape(Context);
  474. if ((Status != 0) || (Context->Exit != FALSE)) {
  475. return Status;
  476. }
  477. continue;
  478. }
  479. Output[OutSize] = Character;
  480. OutSize += 1;
  481. //
  482. // IACs should be doubled going out so they're literal.
  483. //
  484. if (Character == IAC) {
  485. Output[OutSize] = Character;
  486. OutSize += 1;
  487. //
  488. // Convert \r to \r\0.
  489. //
  490. } else if (Character == '\r') {
  491. Output[OutSize] = '\0';
  492. OutSize += 1;
  493. }
  494. }
  495. if (OutSize > 0) {
  496. return TelnetWrite(Context->Socket, Output, Size);
  497. }
  498. return 0;
  499. }
  500. INT
  501. TelnetWriteToOutput (
  502. PTELNET_CONTEXT Context,
  503. size_t Size
  504. )
  505. /*++
  506. Routine Description:
  507. This routine writes data from the socket to standard out, transforming to
  508. work with telnet as needed.
  509. Arguments:
  510. Context - Supplies a pointer to the application context.
  511. Size - Supplies the number of valid bytes in the buffer.
  512. Return Value:
  513. Returns an integer exit code. 0 for success, nonzero otherwise.
  514. --*/
  515. {
  516. UCHAR Character;
  517. size_t CopyStart;
  518. size_t Index;
  519. CopyStart = 0;
  520. for (Index = 0; Index < Size; Index += 1) {
  521. Character = Context->Buffer[Index];
  522. if (Context->State == TelnetStateNormal) {
  523. if (Character == IAC) {
  524. CopyStart = Index;
  525. Context->State = TelnetStateIac;
  526. } else if (Character == '\r') {
  527. CopyStart = Index + 1;
  528. Context->State = TelnetStateCr;
  529. }
  530. continue;
  531. }
  532. switch (Context->State) {
  533. //
  534. // If the previous character was a carraige return and this one is
  535. // null, ignore it.
  536. //
  537. case TelnetStateCr:
  538. Context->State = TelnetStateCopy;
  539. if (Character == '\0') {
  540. break;
  541. }
  542. //
  543. // Fall through.
  544. //
  545. case TelnetStateCopy:
  546. //
  547. // In copy mode, move bytes backwards towards earlier in the buffer
  548. // over bytes that were skipped.
  549. //
  550. if (Character == IAC) {
  551. Context->State = TelnetStateIac;
  552. } else {
  553. Context->Buffer[CopyStart] = Character;
  554. CopyStart += 1;
  555. }
  556. if (Character == '\r') {
  557. Context->State = TelnetStateCr;
  558. }
  559. break;
  560. case TelnetStateIac:
  561. //
  562. // Convert a double IAC down to just one IAC.
  563. //
  564. if (Character == IAC) {
  565. Context->Buffer[CopyStart] = Character;
  566. CopyStart += 1;
  567. Context->State = TelnetStateCopy;
  568. } else {
  569. switch (Character) {
  570. case SB:
  571. Context->State = TelnetStateSubnegotiation1;
  572. break;
  573. case DO:
  574. case DONT:
  575. case WILL:
  576. case WONT:
  577. Context->Wish = Character;
  578. Context->State = TelnetStateOption;
  579. break;
  580. default:
  581. Context->State = TelnetStateCopy;
  582. break;
  583. }
  584. }
  585. break;
  586. case TelnetStateOption:
  587. TelnetHandleOption(Context, Character);
  588. Context->State = TelnetStateCopy;
  589. break;
  590. case TelnetStateSubnegotiation1:
  591. case TelnetStateSubnegotiation2:
  592. TelnetHandleSubnegotiation(Context, Character);
  593. break;
  594. default:
  595. assert(FALSE);
  596. return EINVAL;
  597. }
  598. }
  599. if (Context->State != TelnetStateNormal) {
  600. if (Context->IacSize != 0) {
  601. TelnetFlushIacs(Context);
  602. }
  603. if (Context->State == TelnetStateCopy) {
  604. Context->State = TelnetStateNormal;
  605. }
  606. Size = CopyStart;
  607. }
  608. if (Size != 0) {
  609. return TelnetWrite(STDOUT_FILENO, Context->Buffer, Size);
  610. }
  611. return 0;
  612. }
  613. INT
  614. TelnetWrite (
  615. int Descriptor,
  616. void *Buffer,
  617. size_t Size
  618. )
  619. /*++
  620. Routine Description:
  621. This routine writes the given data out to the given file descriptor in full.
  622. Arguments:
  623. Descriptor - Supplies the descriptor to write to.
  624. Buffer - Supplies a pointer to the buffer to write.
  625. Size - Supplies the number of bytes to write.
  626. Return Value:
  627. Returns an integer exit code. 0 for success, nonzero otherwise.
  628. --*/
  629. {
  630. INT Status;
  631. ssize_t Written;
  632. while (Size != 0) {
  633. Written = write(Descriptor, Buffer, Size);
  634. if (Written <= 0) {
  635. if (errno == EINTR) {
  636. continue;
  637. }
  638. Status = errno;
  639. SwPrintError(Status, NULL, "Failed to write");
  640. return Status;
  641. }
  642. Buffer += Written;
  643. Size -= Written;
  644. }
  645. return 0;
  646. }
  647. INT
  648. TelnetEscape (
  649. PTELNET_CONTEXT Context
  650. )
  651. /*++
  652. Routine Description:
  653. This routine handles local telnet interaction.
  654. Arguments:
  655. Context - Supplies a pointer to the application context.
  656. Return Value:
  657. 0 on success.
  658. Returns an error number on failure.
  659. --*/
  660. {
  661. ssize_t BytesRead;
  662. CHAR Character;
  663. INT Status;
  664. if (Context->Sigint != FALSE) {
  665. SwSetRawInputMode(NULL, NULL);
  666. }
  667. printf("\nConsole escape:\n"
  668. " l - Set line mode.\n"
  669. " c - Set character mode.\n"
  670. " z - Suspend telnet.\n",
  671. " e - Exit telnet.\n\n"
  672. "telnet> ");
  673. do {
  674. BytesRead = read(STDIN_FILENO, &Character, 1);
  675. } while ((BytesRead < 0) && (errno == EINTR));
  676. if (BytesRead <= 0) {
  677. Status = errno;
  678. SwPrintError(Status, NULL, "Failed to read");
  679. return Status;
  680. }
  681. switch (Character) {
  682. case 'l':
  683. TelnetSendDoLineMode(Context);
  684. goto TelnetEscapeEnd;
  685. case 'c':
  686. TelnetSendWillCharMode(Context);
  687. goto TelnetEscapeEnd;
  688. case 'z':
  689. SwRestoreInputMode();
  690. kill(0, SIGTSTP);
  691. SwSetRawInputMode(NULL, NULL);
  692. break;
  693. case 'e':
  694. Context->Exit = TRUE;
  695. goto TelnetEscapeEnd;
  696. }
  697. printf("Continuing...\n");
  698. if (Context->Sigint != FALSE) {
  699. SwRestoreInputMode();
  700. }
  701. TelnetEscapeEnd:
  702. Context->Sigint = FALSE;
  703. return 0;
  704. }
  705. VOID
  706. TelnetHandleOption (
  707. PTELNET_CONTEXT Context,
  708. CHAR Option
  709. )
  710. /*++
  711. Routine Description:
  712. This routine handles an incoming telnet option.
  713. Arguments:
  714. Context - Supplies a pointer to the application context.
  715. Option - Supplies the option to handle.
  716. Return Value:
  717. None.
  718. --*/
  719. {
  720. switch (Option) {
  721. case TELOPT_ECHO:
  722. TelnetHandleEchoOption(Context);
  723. break;
  724. case TELOPT_SGA:
  725. TelnetHandleSgaOption(Context);
  726. break;
  727. case TELOPT_TTYPE:
  728. TelnetHandleTTypeOption(Context);
  729. break;
  730. case TELOPT_NAWS:
  731. TelnetHandleNawsOption(Context);
  732. TelnetSendWindowSize(Context,
  733. Option,
  734. Context->WindowWidth,
  735. Context->WindowHeight);
  736. break;
  737. default:
  738. TelnetHandleUnsupportedOption(Context, Option);
  739. break;
  740. }
  741. return;
  742. }
  743. VOID
  744. TelnetHandleSubnegotiation (
  745. PTELNET_CONTEXT Context,
  746. CHAR Option
  747. )
  748. /*++
  749. Routine Description:
  750. This routine handles a telnet subnegotiation.
  751. Arguments:
  752. Context - Supplies a pointer to the application context.
  753. Option - Supplies the subnegotiation option.
  754. Return Value:
  755. None.
  756. --*/
  757. {
  758. switch (Context->State) {
  759. case TelnetStateSubnegotiation1:
  760. if (Option == IAC) {
  761. Context->State = TelnetStateSubnegotiation2;
  762. break;
  763. }
  764. if ((Option == TELOPT_TTYPE) && (Context->TerminalType != NULL)) {
  765. TelnetSendSuboptIac(Context,
  766. TELOPT_TTYPE,
  767. Context->TerminalType);
  768. }
  769. break;
  770. case TelnetStateSubnegotiation2:
  771. if (Option == SE) {
  772. Context->State = TelnetStateCopy;
  773. break;
  774. }
  775. Context->State = TelnetStateSubnegotiation1;
  776. break;
  777. default:
  778. assert(FALSE);
  779. break;
  780. }
  781. return;
  782. }
  783. VOID
  784. TelnetHandleEchoOption (
  785. PTELNET_CONTEXT Context
  786. )
  787. /*++
  788. Routine Description:
  789. This routine handles an incoming telnet echo option.
  790. Arguments:
  791. Context - Supplies a pointer to the application context.
  792. Return Value:
  793. None.
  794. --*/
  795. {
  796. //
  797. // If the server wants the client to echo, refuse to do it.
  798. //
  799. if (Context->Wish == DO) {
  800. TelnetAddIacWish(Context, WONT, TELOPT_ECHO);
  801. return;
  802. }
  803. if (Context->Wish == DONT) {
  804. return;
  805. }
  806. //
  807. // Do nothing if they already agree with what the client is doing.
  808. //
  809. if ((Context->Flags & TELNET_FLAG_ECHO) != 0) {
  810. if (Context->Wish == WILL) {
  811. return;
  812. }
  813. } else if (Context->Wish == WONT) {
  814. return;
  815. }
  816. if (Context->CharacterMode != TelnetCharacterModeOff) {
  817. Context->Flags ^= TELNET_FLAG_ECHO;
  818. }
  819. if ((Context->Flags & TELNET_FLAG_ECHO) != 0) {
  820. TelnetAddIacWish(Context, DO, TELOPT_ECHO);
  821. } else {
  822. TelnetAddIacWish(Context, DONT, TELOPT_ECHO);
  823. }
  824. TelnetSetConsoleMode(Context);
  825. printf("\n");
  826. return;
  827. }
  828. VOID
  829. TelnetHandleSgaOption (
  830. PTELNET_CONTEXT Context
  831. )
  832. /*++
  833. Routine Description:
  834. This routine handles an incoming telnet Suppress Go Ahead option.
  835. Arguments:
  836. Context - Supplies a pointer to the application context.
  837. Return Value:
  838. None.
  839. --*/
  840. {
  841. //
  842. // Do nothing if it already agrees.
  843. //
  844. if ((Context->Flags & TELNET_FLAG_SUPPRESS_GO_AHEAD) != 0) {
  845. if (Context->Wish == WILL) {
  846. return;
  847. }
  848. } else if (Context->Wish == WONT) {
  849. return;
  850. }
  851. Context->Flags ^= TELNET_FLAG_SUPPRESS_GO_AHEAD;
  852. if ((Context->Flags & TELNET_FLAG_SUPPRESS_GO_AHEAD) != 0) {
  853. TelnetAddIacWish(Context, DO, TELOPT_SGA);
  854. } else {
  855. TelnetAddIacWish(Context, DONT, TELOPT_SGA);
  856. }
  857. return;
  858. }
  859. VOID
  860. TelnetHandleTTypeOption (
  861. PTELNET_CONTEXT Context
  862. )
  863. /*++
  864. Routine Description:
  865. This routine handles an incoming telnet terminal type option.
  866. Arguments:
  867. Context - Supplies a pointer to the application context.
  868. Return Value:
  869. None.
  870. --*/
  871. {
  872. if (Context->TerminalType != NULL) {
  873. TelnetAddIacWish(Context, WILL, TELOPT_TTYPE);
  874. } else {
  875. TelnetAddIacWish(Context, WONT, TELOPT_TTYPE);
  876. }
  877. return;
  878. }
  879. VOID
  880. TelnetHandleNawsOption (
  881. PTELNET_CONTEXT Context
  882. )
  883. /*++
  884. Routine Description:
  885. This routine handles an incoming telnet window size option.
  886. Arguments:
  887. Context - Supplies a pointer to the application context.
  888. Return Value:
  889. None.
  890. --*/
  891. {
  892. TelnetAddIacWish(Context, WILL, TELOPT_NAWS);
  893. return;
  894. }
  895. VOID
  896. TelnetHandleUnsupportedOption (
  897. PTELNET_CONTEXT Context,
  898. CHAR Option
  899. )
  900. /*++
  901. Routine Description:
  902. This routine handles an incoming telnet option that this client does not
  903. recognize.
  904. Arguments:
  905. Context - Supplies a pointer to the application context.
  906. Option - Supplies the unsupported option.
  907. Return Value:
  908. None.
  909. --*/
  910. {
  911. if (Context->Wish == WILL) {
  912. TelnetAddIacWish(Context, WONT, Option);
  913. } else {
  914. TelnetAddIacWish(Context, DONT, Option);
  915. }
  916. return;
  917. }
  918. VOID
  919. TelnetSendWindowSize (
  920. PTELNET_CONTEXT Context,
  921. UCHAR Option,
  922. INT Width,
  923. INT Height
  924. )
  925. /*++
  926. Routine Description:
  927. This routine sends a window size IAC to the server.
  928. Arguments:
  929. Context - Supplies a pointer to the application context.
  930. Option - Supplies the option byte received.
  931. Width - Supplies the window width in characters.
  932. Height - Supplies the window height in characters.
  933. Return Value:
  934. None.
  935. --*/
  936. {
  937. if (Context->IacSize + 9 > TELNET_IAC_BUFFER_SIZE) {
  938. TelnetFlushIacs(Context);
  939. }
  940. TelnetAddIac(Context, IAC);
  941. TelnetAddIac(Context, SB);
  942. TelnetAddIac(Context, Option);
  943. TelnetAddIac(Context, (Width >> BITS_PER_BYTE) & 0xFF);
  944. TelnetAddIac(Context, Width & 0xFF);
  945. TelnetAddIac(Context, (Height >> BITS_PER_BYTE) & 0xFF);
  946. TelnetAddIac(Context, Height & 0xFF);
  947. TelnetAddIac(Context, IAC);
  948. TelnetAddIac(Context, SE);
  949. return;
  950. }
  951. VOID
  952. TelnetSendSuboptIac (
  953. PTELNET_CONTEXT Context,
  954. UCHAR Option,
  955. PSTR String
  956. )
  957. /*++
  958. Routine Description:
  959. This routine sends a suboption IAC control sequence.
  960. Arguments:
  961. Context - Supplies a pointer to the application context.
  962. Option - Supplies the option byte to send.
  963. String - Supplies a pointer to a null-terminated string to send as the
  964. argument.
  965. Return Value:
  966. None.
  967. --*/
  968. {
  969. ULONG Length;
  970. Length = strlen(String) + 6;
  971. if (Context->IacSize + Length > TELNET_IAC_BUFFER_SIZE) {
  972. TelnetFlushIacs(Context);
  973. }
  974. TelnetAddIac(Context, IAC);
  975. TelnetAddIac(Context, SB);
  976. TelnetAddIac(Context, Option);
  977. TelnetAddIac(Context, 0);
  978. while (*String != '\0') {
  979. TelnetAddIac(Context, *String);
  980. String += 1;
  981. }
  982. TelnetAddIac(Context, IAC);
  983. TelnetAddIac(Context, SE);
  984. return;
  985. }
  986. VOID
  987. TelnetSendDoLineMode (
  988. PTELNET_CONTEXT Context
  989. )
  990. /*++
  991. Routine Description:
  992. This routine handles transitioning the client to line mode.
  993. Arguments:
  994. Context - Supplies a pointer to the application context.
  995. Return Value:
  996. None.
  997. --*/
  998. {
  999. Context->CharacterMode = TelnetCharacterModeTry;
  1000. Context->Flags &= ~(TELNET_FLAG_ECHO | TELNET_FLAG_SUPPRESS_GO_AHEAD);
  1001. TelnetSetConsoleMode(Context);
  1002. TelnetAddIacWish(Context, DONT, TELOPT_ECHO);
  1003. TelnetAddIacWish(Context, DONT, TELOPT_SGA);
  1004. TelnetFlushIacs(Context);
  1005. return;
  1006. }
  1007. VOID
  1008. TelnetSendWillCharMode (
  1009. PTELNET_CONTEXT Context
  1010. )
  1011. /*++
  1012. Routine Description:
  1013. This routine handles transitioning the client to character mode.
  1014. Arguments:
  1015. Context - Supplies a pointer to the application context.
  1016. Return Value:
  1017. None.
  1018. --*/
  1019. {
  1020. Context->CharacterMode = TelnetCharacterModeTry;
  1021. Context->Flags |= TELNET_FLAG_ECHO | TELNET_FLAG_SUPPRESS_GO_AHEAD;
  1022. TelnetSetConsoleMode(Context);
  1023. TelnetAddIacWish(Context, DO, TELOPT_ECHO);
  1024. TelnetAddIacWish(Context, DO, TELOPT_SGA);
  1025. TelnetFlushIacs(Context);
  1026. return;
  1027. }
  1028. VOID
  1029. TelnetSetConsoleMode (
  1030. PTELNET_CONTEXT Context
  1031. )
  1032. /*++
  1033. Routine Description:
  1034. This routine sets the current console mode to either raw mode or cooked
  1035. mode depending on the flags in the context.
  1036. Arguments:
  1037. Context - Supplies a pointer to the application context.
  1038. Return Value:
  1039. None.
  1040. --*/
  1041. {
  1042. if ((Context->Flags & TELNET_FLAG_ECHO) != 0) {
  1043. if (Context->CharacterMode == TelnetCharacterModeTry) {
  1044. Context->CharacterMode = TelnetCharacterModeOn;
  1045. printf(TELNET_ENTERING_MODE_FORMAT, "character", "^]");
  1046. SwSetRawInputMode(NULL, NULL);
  1047. }
  1048. } else {
  1049. if (Context->CharacterMode != TelnetCharacterModeOff) {
  1050. Context->CharacterMode = TelnetCharacterModeOff;
  1051. printf(TELNET_ENTERING_MODE_FORMAT, "line", "^C");
  1052. SwRestoreInputMode();
  1053. }
  1054. }
  1055. return;
  1056. }
  1057. VOID
  1058. TelnetAddIacWish (
  1059. PTELNET_CONTEXT Context,
  1060. UCHAR Wish,
  1061. UCHAR Option
  1062. )
  1063. /*++
  1064. Routine Description:
  1065. This routine sends an IAC DO/DONT/WILL/WONT sequence to the outgoing IAC
  1066. buffer.
  1067. Arguments:
  1068. Context - Supplies a pointer to the application context.
  1069. Wish - Supplies the wish, which should be DO, DONT, WILL, or WONT (defined
  1070. in telnet.h).
  1071. Option - Supplies the option to wish for or against.
  1072. Return Value:
  1073. None.
  1074. --*/
  1075. {
  1076. if (Context->IacSize + 3 > TELNET_IAC_BUFFER_SIZE) {
  1077. TelnetFlushIacs(Context);
  1078. }
  1079. TelnetAddIac(Context, IAC);
  1080. TelnetAddIac(Context, Wish);
  1081. TelnetAddIac(Context, Option);
  1082. return;
  1083. }
  1084. VOID
  1085. TelnetAddIac (
  1086. PTELNET_CONTEXT Context,
  1087. UCHAR Character
  1088. )
  1089. /*++
  1090. Routine Description:
  1091. This routine adds a character to the IAC buffer.
  1092. Arguments:
  1093. Context - Supplies a pointer to the application context.
  1094. Character - Supplies the character to add.
  1095. Return Value:
  1096. None.
  1097. --*/
  1098. {
  1099. if (Context->IacSize >= TELNET_IAC_BUFFER_SIZE) {
  1100. TelnetFlushIacs(Context);
  1101. }
  1102. assert(Context->IacSize < TELNET_IAC_BUFFER_SIZE);
  1103. Context->IacBuffer[Context->IacSize] = Character;
  1104. Context->IacSize += 1;
  1105. return;
  1106. }
  1107. VOID
  1108. TelnetFlushIacs (
  1109. PTELNET_CONTEXT Context
  1110. )
  1111. /*++
  1112. Routine Description:
  1113. This routine writes the IAC buffer out to the socket.
  1114. Arguments:
  1115. Context - Supplies a pointer to the application context.
  1116. Return Value:
  1117. None.
  1118. --*/
  1119. {
  1120. TelnetWrite(Context->Socket, Context->IacBuffer, Context->IacSize);
  1121. Context->IacSize = 0;
  1122. return;
  1123. }