telnet.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579
  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. CHAR Character;
  662. INT Status;
  663. if (Context->Sigint != FALSE) {
  664. SwSetRawInputMode(NULL, NULL);
  665. }
  666. printf("\nConsole escape:\n"
  667. " l - Set line mode.\n"
  668. " c - Set character mode.\n"
  669. " z - Suspend telnet.\n",
  670. " e - Exit telnet.\n\n"
  671. "telnet> ");
  672. if (read(STDIN_FILENO, &Character, 1) <= 0) {
  673. Status = errno;
  674. SwPrintError(Status, NULL, "Failed to read");
  675. return Status;
  676. }
  677. switch (Character) {
  678. case 'l':
  679. TelnetSendDoLineMode(Context);
  680. goto TelnetEscapeEnd;
  681. case 'c':
  682. TelnetSendWillCharMode(Context);
  683. goto TelnetEscapeEnd;
  684. case 'z':
  685. SwRestoreInputMode();
  686. kill(0, SIGTSTP);
  687. SwSetRawInputMode(NULL, NULL);
  688. break;
  689. case 'e':
  690. Context->Exit = TRUE;
  691. goto TelnetEscapeEnd;
  692. }
  693. printf("Continuing...\n");
  694. if (Context->Sigint != FALSE) {
  695. SwRestoreInputMode();
  696. }
  697. TelnetEscapeEnd:
  698. Context->Sigint = FALSE;
  699. return 0;
  700. }
  701. VOID
  702. TelnetHandleOption (
  703. PTELNET_CONTEXT Context,
  704. CHAR Option
  705. )
  706. /*++
  707. Routine Description:
  708. This routine handles an incoming telnet option.
  709. Arguments:
  710. Context - Supplies a pointer to the application context.
  711. Option - Supplies the option to handle.
  712. Return Value:
  713. None.
  714. --*/
  715. {
  716. switch (Option) {
  717. case TELOPT_ECHO:
  718. TelnetHandleEchoOption(Context);
  719. break;
  720. case TELOPT_SGA:
  721. TelnetHandleSgaOption(Context);
  722. break;
  723. case TELOPT_TTYPE:
  724. TelnetHandleTTypeOption(Context);
  725. break;
  726. case TELOPT_NAWS:
  727. TelnetHandleNawsOption(Context);
  728. TelnetSendWindowSize(Context,
  729. Option,
  730. Context->WindowWidth,
  731. Context->WindowHeight);
  732. break;
  733. default:
  734. TelnetHandleUnsupportedOption(Context, Option);
  735. break;
  736. }
  737. return;
  738. }
  739. VOID
  740. TelnetHandleSubnegotiation (
  741. PTELNET_CONTEXT Context,
  742. CHAR Option
  743. )
  744. /*++
  745. Routine Description:
  746. This routine handles a telnet subnegotiation.
  747. Arguments:
  748. Context - Supplies a pointer to the application context.
  749. Option - Supplies the subnegotiation option.
  750. Return Value:
  751. None.
  752. --*/
  753. {
  754. switch (Context->State) {
  755. case TelnetStateSubnegotiation1:
  756. if (Option == IAC) {
  757. Context->State = TelnetStateSubnegotiation2;
  758. break;
  759. }
  760. if ((Option == TELOPT_TTYPE) && (Context->TerminalType != NULL)) {
  761. TelnetSendSuboptIac(Context,
  762. TELOPT_TTYPE,
  763. Context->TerminalType);
  764. }
  765. break;
  766. case TelnetStateSubnegotiation2:
  767. if (Option == SE) {
  768. Context->State = TelnetStateCopy;
  769. break;
  770. }
  771. Context->State = TelnetStateSubnegotiation1;
  772. break;
  773. default:
  774. assert(FALSE);
  775. break;
  776. }
  777. return;
  778. }
  779. VOID
  780. TelnetHandleEchoOption (
  781. PTELNET_CONTEXT Context
  782. )
  783. /*++
  784. Routine Description:
  785. This routine handles an incoming telnet echo option.
  786. Arguments:
  787. Context - Supplies a pointer to the application context.
  788. Return Value:
  789. None.
  790. --*/
  791. {
  792. //
  793. // If the server wants the client to echo, refuse to do it.
  794. //
  795. if (Context->Wish == DO) {
  796. TelnetAddIacWish(Context, WONT, TELOPT_ECHO);
  797. return;
  798. }
  799. if (Context->Wish == DONT) {
  800. return;
  801. }
  802. //
  803. // Do nothing if they already agree with what the client is doing.
  804. //
  805. if ((Context->Flags & TELNET_FLAG_ECHO) != 0) {
  806. if (Context->Wish == WILL) {
  807. return;
  808. }
  809. } else if (Context->Wish == WONT) {
  810. return;
  811. }
  812. if (Context->CharacterMode != TelnetCharacterModeOff) {
  813. Context->Flags ^= TELNET_FLAG_ECHO;
  814. }
  815. if ((Context->Flags & TELNET_FLAG_ECHO) != 0) {
  816. TelnetAddIacWish(Context, DO, TELOPT_ECHO);
  817. } else {
  818. TelnetAddIacWish(Context, DONT, TELOPT_ECHO);
  819. }
  820. TelnetSetConsoleMode(Context);
  821. printf("\n");
  822. return;
  823. }
  824. VOID
  825. TelnetHandleSgaOption (
  826. PTELNET_CONTEXT Context
  827. )
  828. /*++
  829. Routine Description:
  830. This routine handles an incoming telnet Suppress Go Ahead option.
  831. Arguments:
  832. Context - Supplies a pointer to the application context.
  833. Return Value:
  834. None.
  835. --*/
  836. {
  837. //
  838. // Do nothing if it already agrees.
  839. //
  840. if ((Context->Flags & TELNET_FLAG_SUPPRESS_GO_AHEAD) != 0) {
  841. if (Context->Wish == WILL) {
  842. return;
  843. }
  844. } else if (Context->Wish == WONT) {
  845. return;
  846. }
  847. Context->Flags ^= TELNET_FLAG_SUPPRESS_GO_AHEAD;
  848. if ((Context->Flags & TELNET_FLAG_SUPPRESS_GO_AHEAD) != 0) {
  849. TelnetAddIacWish(Context, DO, TELOPT_SGA);
  850. } else {
  851. TelnetAddIacWish(Context, DONT, TELOPT_SGA);
  852. }
  853. return;
  854. }
  855. VOID
  856. TelnetHandleTTypeOption (
  857. PTELNET_CONTEXT Context
  858. )
  859. /*++
  860. Routine Description:
  861. This routine handles an incoming telnet terminal type option.
  862. Arguments:
  863. Context - Supplies a pointer to the application context.
  864. Return Value:
  865. None.
  866. --*/
  867. {
  868. if (Context->TerminalType != NULL) {
  869. TelnetAddIacWish(Context, WILL, TELOPT_TTYPE);
  870. } else {
  871. TelnetAddIacWish(Context, WONT, TELOPT_TTYPE);
  872. }
  873. return;
  874. }
  875. VOID
  876. TelnetHandleNawsOption (
  877. PTELNET_CONTEXT Context
  878. )
  879. /*++
  880. Routine Description:
  881. This routine handles an incoming telnet window size option.
  882. Arguments:
  883. Context - Supplies a pointer to the application context.
  884. Return Value:
  885. None.
  886. --*/
  887. {
  888. TelnetAddIacWish(Context, WILL, TELOPT_NAWS);
  889. return;
  890. }
  891. VOID
  892. TelnetHandleUnsupportedOption (
  893. PTELNET_CONTEXT Context,
  894. CHAR Option
  895. )
  896. /*++
  897. Routine Description:
  898. This routine handles an incoming telnet option that this client does not
  899. recognize.
  900. Arguments:
  901. Context - Supplies a pointer to the application context.
  902. Option - Supplies the unsupported option.
  903. Return Value:
  904. None.
  905. --*/
  906. {
  907. if (Context->Wish == WILL) {
  908. TelnetAddIacWish(Context, WONT, Option);
  909. } else {
  910. TelnetAddIacWish(Context, DONT, Option);
  911. }
  912. return;
  913. }
  914. VOID
  915. TelnetSendWindowSize (
  916. PTELNET_CONTEXT Context,
  917. UCHAR Option,
  918. INT Width,
  919. INT Height
  920. )
  921. /*++
  922. Routine Description:
  923. This routine sends a window size IAC to the server.
  924. Arguments:
  925. Context - Supplies a pointer to the application context.
  926. Option - Supplies the option byte received.
  927. Width - Supplies the window width in characters.
  928. Height - Supplies the window height in characters.
  929. Return Value:
  930. None.
  931. --*/
  932. {
  933. if (Context->IacSize + 9 > TELNET_IAC_BUFFER_SIZE) {
  934. TelnetFlushIacs(Context);
  935. }
  936. TelnetAddIac(Context, IAC);
  937. TelnetAddIac(Context, SB);
  938. TelnetAddIac(Context, Option);
  939. TelnetAddIac(Context, (Width >> BITS_PER_BYTE) & 0xFF);
  940. TelnetAddIac(Context, Width & 0xFF);
  941. TelnetAddIac(Context, (Height >> BITS_PER_BYTE) & 0xFF);
  942. TelnetAddIac(Context, Height & 0xFF);
  943. TelnetAddIac(Context, IAC);
  944. TelnetAddIac(Context, SE);
  945. return;
  946. }
  947. VOID
  948. TelnetSendSuboptIac (
  949. PTELNET_CONTEXT Context,
  950. UCHAR Option,
  951. PSTR String
  952. )
  953. /*++
  954. Routine Description:
  955. This routine sends a suboption IAC control sequence.
  956. Arguments:
  957. Context - Supplies a pointer to the application context.
  958. Option - Supplies the option byte to send.
  959. String - Supplies a pointer to a null-terminated string to send as the
  960. argument.
  961. Return Value:
  962. None.
  963. --*/
  964. {
  965. ULONG Length;
  966. Length = strlen(String) + 6;
  967. if (Context->IacSize + Length > TELNET_IAC_BUFFER_SIZE) {
  968. TelnetFlushIacs(Context);
  969. }
  970. TelnetAddIac(Context, IAC);
  971. TelnetAddIac(Context, SB);
  972. TelnetAddIac(Context, Option);
  973. TelnetAddIac(Context, 0);
  974. while (*String != '\0') {
  975. TelnetAddIac(Context, *String);
  976. String += 1;
  977. }
  978. TelnetAddIac(Context, IAC);
  979. TelnetAddIac(Context, SE);
  980. return;
  981. }
  982. VOID
  983. TelnetSendDoLineMode (
  984. PTELNET_CONTEXT Context
  985. )
  986. /*++
  987. Routine Description:
  988. This routine handles transitioning the client to line mode.
  989. Arguments:
  990. Context - Supplies a pointer to the application context.
  991. Return Value:
  992. None.
  993. --*/
  994. {
  995. Context->CharacterMode = TelnetCharacterModeTry;
  996. Context->Flags &= ~(TELNET_FLAG_ECHO | TELNET_FLAG_SUPPRESS_GO_AHEAD);
  997. TelnetSetConsoleMode(Context);
  998. TelnetAddIacWish(Context, DONT, TELOPT_ECHO);
  999. TelnetAddIacWish(Context, DONT, TELOPT_SGA);
  1000. TelnetFlushIacs(Context);
  1001. return;
  1002. }
  1003. VOID
  1004. TelnetSendWillCharMode (
  1005. PTELNET_CONTEXT Context
  1006. )
  1007. /*++
  1008. Routine Description:
  1009. This routine handles transitioning the client to character mode.
  1010. Arguments:
  1011. Context - Supplies a pointer to the application context.
  1012. Return Value:
  1013. None.
  1014. --*/
  1015. {
  1016. Context->CharacterMode = TelnetCharacterModeTry;
  1017. Context->Flags |= TELNET_FLAG_ECHO | TELNET_FLAG_SUPPRESS_GO_AHEAD;
  1018. TelnetSetConsoleMode(Context);
  1019. TelnetAddIacWish(Context, DO, TELOPT_ECHO);
  1020. TelnetAddIacWish(Context, DO, TELOPT_SGA);
  1021. TelnetFlushIacs(Context);
  1022. return;
  1023. }
  1024. VOID
  1025. TelnetSetConsoleMode (
  1026. PTELNET_CONTEXT Context
  1027. )
  1028. /*++
  1029. Routine Description:
  1030. This routine sets the current console mode to either raw mode or cooked
  1031. mode depending on the flags in the context.
  1032. Arguments:
  1033. Context - Supplies a pointer to the application context.
  1034. Return Value:
  1035. None.
  1036. --*/
  1037. {
  1038. if ((Context->Flags & TELNET_FLAG_ECHO) != 0) {
  1039. if (Context->CharacterMode == TelnetCharacterModeTry) {
  1040. Context->CharacterMode = TelnetCharacterModeOn;
  1041. printf(TELNET_ENTERING_MODE_FORMAT, "character", "^]");
  1042. SwSetRawInputMode(NULL, NULL);
  1043. }
  1044. } else {
  1045. if (Context->CharacterMode != TelnetCharacterModeOff) {
  1046. Context->CharacterMode = TelnetCharacterModeOff;
  1047. printf(TELNET_ENTERING_MODE_FORMAT, "line", "^C");
  1048. SwRestoreInputMode();
  1049. }
  1050. }
  1051. return;
  1052. }
  1053. VOID
  1054. TelnetAddIacWish (
  1055. PTELNET_CONTEXT Context,
  1056. UCHAR Wish,
  1057. UCHAR Option
  1058. )
  1059. /*++
  1060. Routine Description:
  1061. This routine sends an IAC DO/DONT/WILL/WONT sequence to the outgoing IAC
  1062. buffer.
  1063. Arguments:
  1064. Context - Supplies a pointer to the application context.
  1065. Wish - Supplies the wish, which should be DO, DONT, WILL, or WONT (defined
  1066. in telnet.h).
  1067. Option - Supplies the option to wish for or against.
  1068. Return Value:
  1069. None.
  1070. --*/
  1071. {
  1072. if (Context->IacSize + 3 > TELNET_IAC_BUFFER_SIZE) {
  1073. TelnetFlushIacs(Context);
  1074. }
  1075. TelnetAddIac(Context, IAC);
  1076. TelnetAddIac(Context, Wish);
  1077. TelnetAddIac(Context, Option);
  1078. return;
  1079. }
  1080. VOID
  1081. TelnetAddIac (
  1082. PTELNET_CONTEXT Context,
  1083. UCHAR Character
  1084. )
  1085. /*++
  1086. Routine Description:
  1087. This routine adds a character to the IAC buffer.
  1088. Arguments:
  1089. Context - Supplies a pointer to the application context.
  1090. Character - Supplies the character to add.
  1091. Return Value:
  1092. None.
  1093. --*/
  1094. {
  1095. if (Context->IacSize >= TELNET_IAC_BUFFER_SIZE) {
  1096. TelnetFlushIacs(Context);
  1097. }
  1098. assert(Context->IacSize < TELNET_IAC_BUFFER_SIZE);
  1099. Context->IacBuffer[Context->IacSize] = Character;
  1100. Context->IacSize += 1;
  1101. return;
  1102. }
  1103. VOID
  1104. TelnetFlushIacs (
  1105. PTELNET_CONTEXT Context
  1106. )
  1107. /*++
  1108. Routine Description:
  1109. This routine writes the IAC buffer out to the socket.
  1110. Arguments:
  1111. Context - Supplies a pointer to the application context.
  1112. Return Value:
  1113. None.
  1114. --*/
  1115. {
  1116. TelnetWrite(Context->Socket, Context->IacBuffer, Context->IacSize);
  1117. Context->IacSize = 0;
  1118. return;
  1119. }