1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579 |
- /*++
- Copyright (c) 2015 Minoca Corp. All Rights Reserved
- Module Name:
- telnet.c
- Abstract:
- This module implements a very simple telnet client.
- Author:
- Evan Green 4-Oct-2015
- Environment:
- POSIX
- --*/
- //
- // ------------------------------------------------------------------- Includes
- //
- //
- // Add some definitions to get the strings out of telnet.h. The telnet daemon
- // also relies on the client having the string declarations here.
- //
- #define TELCMDS 1
- #include <minoca/lib/types.h>
- #include <arpa/telnet.h>
- #include <assert.h>
- #include <errno.h>
- #include <getopt.h>
- #include <netdb.h>
- #include <poll.h>
- #include <signal.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <unistd.h>
- #include "swlib.h"
- //
- // --------------------------------------------------------------------- Macros
- //
- //
- // ---------------------------------------------------------------- Definitions
- //
- #define TELNET_VERSION_MAJOR 1
- #define TELNET_VERSION_MINOR 0
- #define TELNET_USAGE \
- "usage: telnet [-l user] host [port]\n" \
- "The telnet utility implements a simple telnet client. Options are:\n" \
- " --help -- Show this help text and exit.\n" \
- " --version -- Print the application version information and exit.\n"
- #define TELNET_OPTIONS_STRING "l:hv"
- #define TELNET_ENTERING_MODE_FORMAT \
- "\nEntering %s mode. Escape character is %s.\n"
- #define TELNET_BUFFER_SIZE 256
- #define TELNET_IAC_BUFFER_SIZE 64
- //
- // Define the escape character, ^].
- //
- #define TELNET_ESCAPE 0x1D
- #define TELNET_FLAG_ECHO 0x00000001
- #define TELNET_FLAG_SUPPRESS_GO_AHEAD 0x00000002
- //
- // ------------------------------------------------------ Data Type Definitions
- //
- typedef enum _TELNET_STATE {
- TelnetStateInvalid,
- TelnetStateNormal,
- TelnetStateCopy,
- TelnetStateIac,
- TelnetStateOption,
- TelnetStateSubnegotiation1,
- TelnetStateSubnegotiation2,
- TelnetStateCr
- } TELNET_STATE, *PTELNET_STATE;
- typedef enum _TELNET_CHARACTER_MODE {
- TelnetCharacterModeTry,
- TelnetCharacterModeOn,
- TelnetCharacterModeOff
- } TELNET_CHARACTER_MODE, *PTELNET_CHARACTER_MODE;
- /*++
- Structure Description:
- This structure defines the context for an instantiation of the telnet
- client.
- Members:
- WindowHeight - Stores the window height in characters.
- WindowWidth - Stores the window width in characters.
- TerminalType - Stores the terminal type.
- Socket - Stores a pointer to the socket.
- Poll - Stores the poll descriptors.
- Sigint - Stores a boolean indicating if a pending sigint occurred.
- Exit - Stores a boolean indicating an exit is requested.
- Buffer - Stores the data buffer.
- State - Stores the current telnet command state.
- Wish - Stores the option that the remote side is asking/telling this client
- about.
- Flags - Stores telnet flags. See TELNET_FLAG_* definitions.
- CharacterMode - Stores the character mode negotiation state.
- IacBuffer - Stores the buffer used to send telnet options and
- subnegotiations.
- IacSize - Stores the size of the IAC buffer.
- --*/
- typedef struct _TELNET_CONTEXT {
- int WindowHeight;
- int WindowWidth;
- PSTR TerminalType;
- int Socket;
- struct pollfd Poll[2];
- BOOL Sigint;
- BOOL Exit;
- UCHAR Buffer[TELNET_BUFFER_SIZE];
- TELNET_STATE State;
- UCHAR Wish;
- ULONG Flags;
- TELNET_CHARACTER_MODE CharacterMode;
- UCHAR IacBuffer[TELNET_IAC_BUFFER_SIZE];
- ULONG IacSize;
- } TELNET_CONTEXT, *PTELNET_CONTEXT;
- //
- // ----------------------------------------------- Internal Function Prototypes
- //
- void
- TelnetSigintHandler (
- int Signal
- );
- INT
- TelnetWriteToSocket (
- PTELNET_CONTEXT Context,
- size_t Size
- );
- INT
- TelnetWriteToOutput (
- PTELNET_CONTEXT Context,
- size_t Size
- );
- INT
- TelnetWrite (
- int Descriptor,
- void *Buffer,
- size_t Size
- );
- INT
- TelnetEscape (
- PTELNET_CONTEXT Context
- );
- VOID
- TelnetHandleOption (
- PTELNET_CONTEXT Context,
- CHAR Option
- );
- VOID
- TelnetHandleSubnegotiation (
- PTELNET_CONTEXT Context,
- CHAR Option
- );
- VOID
- TelnetHandleEchoOption (
- PTELNET_CONTEXT Context
- );
- VOID
- TelnetHandleSgaOption (
- PTELNET_CONTEXT Context
- );
- VOID
- TelnetHandleTTypeOption (
- PTELNET_CONTEXT Context
- );
- VOID
- TelnetHandleNawsOption (
- PTELNET_CONTEXT Context
- );
- VOID
- TelnetHandleUnsupportedOption (
- PTELNET_CONTEXT Context,
- CHAR Option
- );
- VOID
- TelnetSendWindowSize (
- PTELNET_CONTEXT Context,
- UCHAR Option,
- INT Width,
- INT Height
- );
- VOID
- TelnetSendSuboptIac (
- PTELNET_CONTEXT Context,
- UCHAR Option,
- PSTR String
- );
- VOID
- TelnetSendDoLineMode (
- PTELNET_CONTEXT Context
- );
- VOID
- TelnetSendWillCharMode (
- PTELNET_CONTEXT Context
- );
- VOID
- TelnetSetConsoleMode (
- PTELNET_CONTEXT Context
- );
- VOID
- TelnetAddIacWish (
- PTELNET_CONTEXT Context,
- UCHAR Wish,
- UCHAR Option
- );
- VOID
- TelnetAddIac (
- PTELNET_CONTEXT Context,
- UCHAR Character
- );
- VOID
- TelnetFlushIacs (
- PTELNET_CONTEXT Context
- );
- //
- // -------------------------------------------------------------------- Globals
- //
- struct option TelnetLongOptions[] = {
- {"help", no_argument, 0, 'h'},
- {"version", no_argument, 0, 'V'},
- {NULL, 0, 0, 0},
- };
- PTELNET_CONTEXT TelnetGlobalContext;
- //
- // ------------------------------------------------------------------ Functions
- //
- INT
- TelnetMain (
- INT ArgumentCount,
- CHAR **Arguments
- )
- /*++
- Routine Description:
- This routine is the main entry point for the telnet utility.
- Arguments:
- ArgumentCount - Supplies the number of command line arguments the program
- was invoked with.
- Arguments - Supplies a tokenized array of command line arguments.
- Return Value:
- Returns an integer exit code. 0 for success, nonzero otherwise.
- --*/
- {
- struct sigaction Action;
- struct addrinfo *Address;
- struct addrinfo *AddressInfo;
- ULONG ArgumentIndex;
- ssize_t BytesDone;
- TELNET_CONTEXT Context;
- PSTR Host;
- INT Option;
- struct sigaction OriginalAction;
- PSTR PortString;
- int Status;
- int Value;
- AddressInfo = NULL;
- memset(&Context, 0, sizeof(TELNET_CONTEXT));
- Context.State = TelnetStateNormal;
- Context.Socket = -1;
- Context.TerminalType = getenv("TERM");
- SwGetTerminalDimensions(&(Context.WindowWidth), &(Context.WindowHeight));
- SwSetRawInputMode(NULL, NULL);
- TelnetGlobalContext = &Context;
- memset(&Action, 0, sizeof(struct sigaction));
- Action.sa_handler = TelnetSigintHandler;
- sigaction(SIGINT, &Action, &OriginalAction);
- //
- // Process the control arguments.
- //
- while (TRUE) {
- Option = getopt_long(ArgumentCount,
- Arguments,
- TELNET_OPTIONS_STRING,
- TelnetLongOptions,
- NULL);
- if (Option == -1) {
- break;
- }
- if ((Option == '?') || (Option == ':')) {
- Status = 1;
- goto MainEnd;
- }
- switch (Option) {
- case 'V':
- SwPrintVersion(TELNET_VERSION_MAJOR, TELNET_VERSION_MINOR);
- return 1;
- case 'h':
- printf(TELNET_USAGE);
- return 1;
- default:
- assert(FALSE);
- Status = 1;
- goto MainEnd;
- }
- }
- ArgumentIndex = optind;
- if (ArgumentIndex >= ArgumentCount) {
- SwPrintError(0, NULL, "Argument required. Try --help for usage");
- Status = 1;
- goto MainEnd;
- }
- PortString = "23";
- Host = Arguments[ArgumentIndex];
- ArgumentIndex += 1;
- if (ArgumentIndex < ArgumentCount) {
- PortString = Arguments[ArgumentIndex];
- ArgumentIndex += 1;
- if (ArgumentIndex < ArgumentCount) {
- SwPrintError(0, NULL, "Too many arguments");
- Status = 1;
- goto MainEnd;
- }
- }
- Status = getaddrinfo(Host, PortString, NULL, &AddressInfo);
- if (Status != 0) {
- SwPrintError(0,
- NULL,
- "Cannot resolve %s:%s: %s.\n",
- Host,
- PortString,
- gai_strerror(Status));
- Status = errno;
- if (Status == 0) {
- Status = 1;
- }
- goto MainEnd;
- }
- Address = AddressInfo;
- while (Address != NULL) {
- if (Address->ai_socktype == SOCK_STREAM) {
- Context.Socket = socket(Address->ai_family,
- Address->ai_socktype,
- Address->ai_protocol);
- if (Context.Socket < 0) {
- Status = errno;
- SwPrintError(Status, NULL, "Unable to create socket");
- goto MainEnd;
- }
- Status = connect(Context.Socket,
- Address->ai_addr,
- Address->ai_addrlen);
- if (Status != 0) {
- Status = errno;
- SwPrintError(Status, Host, "Unable to connect");
- close(Context.Socket);
- Context.Socket = -1;
- }
- break;
- }
- Address = Address->ai_next;
- }
- if (Context.Socket < 0) {
- Status = 1;
- SwPrintError(0, Host, "Connection failed");
- goto MainEnd;
- }
- Value = 1;
- setsockopt(Context.Socket, SOL_SOCKET, SO_KEEPALIVE, &Value, sizeof(Value));
- Context.Poll[0].fd = STDIN_FILENO;
- Context.Poll[0].events = POLLIN;
- Context.Poll[1].fd = Context.Socket;
- Context.Poll[1].events = POLLIN;
- //
- // This is the main loop, which shuttles data.
- //
- while (Context.Exit == FALSE) {
- if (poll(Context.Poll, 2, -1) < 0) {
- if (Context.Sigint != 0) {
- TelnetEscape(&Context);
- } else {
- if (errno != 0) {
- Status = errno;
- if (errno == EINTR) {
- continue;
- }
- SwPrintError(Status, NULL, "Error");
- break;
- }
- }
- }
- //
- // Check standard in heading out to the socket.
- //
- if ((Context.Poll[0].revents & POLLIN) != 0) {
- do {
- BytesDone = read(STDIN_FILENO,
- Context.Buffer,
- TELNET_BUFFER_SIZE);
- } while ((BytesDone <= 0) && (errno == EINTR));
- if (BytesDone == 0) {
- break;
- } else if (BytesDone < 0) {
- Status = errno;
- SwPrintError(Status, NULL, "Failed to read");
- goto MainEnd;
- }
- TelnetWriteToSocket(&Context, BytesDone);
- }
- //
- // Check the socket headed off to standard out.
- //
- if ((Context.Poll[1].revents & POLLIN) != 0) {
- do {
- BytesDone = read(Context.Socket,
- Context.Buffer,
- TELNET_BUFFER_SIZE);
- } while ((BytesDone <= 0) && (errno == EINTR));
- if (BytesDone == 0) {
- break;
- } else if (BytesDone < 0) {
- Status = errno;
- SwPrintError(Status, NULL, "Failed to read");
- goto MainEnd;
- }
- TelnetWriteToOutput(&Context, BytesDone);
- }
- }
- shutdown(Context.Socket, SHUT_RDWR);
- MainEnd:
- sigaction(SIGINT, &OriginalAction, NULL);
- TelnetGlobalContext = NULL;
- if (Context.Socket >= 0) {
- close(Context.Socket);
- }
- if (AddressInfo != NULL) {
- freeaddrinfo(AddressInfo);
- }
- SwRestoreInputMode();
- return Status;
- }
- //
- // --------------------------------------------------------- Internal Functions
- //
- void
- TelnetSigintHandler (
- int Signal
- )
- /*++
- Routine Description:
- This routine handles SIGINT signals, simply marking that they occurred.
- Arguments:
- Signal - Supplies the signal number that fired, which in this case is
- SIGINT.
- Return Value:
- None.
- --*/
- {
- TelnetGlobalContext->Sigint = TRUE;
- return;
- }
- INT
- TelnetWriteToSocket (
- PTELNET_CONTEXT Context,
- size_t Size
- )
- /*++
- Routine Description:
- This routine writes data from standard input out to the socket,
- transforming to work with telnet as needed.
- Arguments:
- Context - Supplies a pointer to the application context.
- Size - Supplies the number of valid bytes in the buffer.
- Return Value:
- Returns an integer exit code. 0 for success, nonzero otherwise.
- --*/
- {
- UCHAR Character;
- UINTN Index;
- PUCHAR Input;
- UCHAR Output[TELNET_BUFFER_SIZE * 2];
- size_t OutSize;
- INT Status;
- Input = Context->Buffer;
- OutSize = 0;
- for (Index = 0; Index < Size; Index += 1) {
- Character = *Input;
- if (Character == TELNET_ESCAPE) {
- Status = TelnetEscape(Context);
- if ((Status != 0) || (Context->Exit != FALSE)) {
- return Status;
- }
- continue;
- }
- Output[OutSize] = Character;
- OutSize += 1;
- //
- // IACs should be doubled going out so they're literal.
- //
- if (Character == IAC) {
- Output[OutSize] = Character;
- OutSize += 1;
- //
- // Convert \r to \r\0.
- //
- } else if (Character == '\r') {
- Output[OutSize] = '\0';
- OutSize += 1;
- }
- }
- if (OutSize > 0) {
- return TelnetWrite(Context->Socket, Output, Size);
- }
- return 0;
- }
- INT
- TelnetWriteToOutput (
- PTELNET_CONTEXT Context,
- size_t Size
- )
- /*++
- Routine Description:
- This routine writes data from the socket to standard out, transforming to
- work with telnet as needed.
- Arguments:
- Context - Supplies a pointer to the application context.
- Size - Supplies the number of valid bytes in the buffer.
- Return Value:
- Returns an integer exit code. 0 for success, nonzero otherwise.
- --*/
- {
- UCHAR Character;
- size_t CopyStart;
- size_t Index;
- CopyStart = 0;
- for (Index = 0; Index < Size; Index += 1) {
- Character = Context->Buffer[Index];
- if (Context->State == TelnetStateNormal) {
- if (Character == IAC) {
- CopyStart = Index;
- Context->State = TelnetStateIac;
- } else if (Character == '\r') {
- CopyStart = Index + 1;
- Context->State = TelnetStateCr;
- }
- continue;
- }
- switch (Context->State) {
- //
- // If the previous character was a carraige return and this one is
- // null, ignore it.
- //
- case TelnetStateCr:
- Context->State = TelnetStateCopy;
- if (Character == '\0') {
- break;
- }
- //
- // Fall through.
- //
- case TelnetStateCopy:
- //
- // In copy mode, move bytes backwards towards earlier in the buffer
- // over bytes that were skipped.
- //
- if (Character == IAC) {
- Context->State = TelnetStateIac;
- } else {
- Context->Buffer[CopyStart] = Character;
- CopyStart += 1;
- }
- if (Character == '\r') {
- Context->State = TelnetStateCr;
- }
- break;
- case TelnetStateIac:
- //
- // Convert a double IAC down to just one IAC.
- //
- if (Character == IAC) {
- Context->Buffer[CopyStart] = Character;
- CopyStart += 1;
- Context->State = TelnetStateCopy;
- } else {
- switch (Character) {
- case SB:
- Context->State = TelnetStateSubnegotiation1;
- break;
- case DO:
- case DONT:
- case WILL:
- case WONT:
- Context->Wish = Character;
- Context->State = TelnetStateOption;
- break;
- default:
- Context->State = TelnetStateCopy;
- break;
- }
- }
- break;
- case TelnetStateOption:
- TelnetHandleOption(Context, Character);
- Context->State = TelnetStateCopy;
- break;
- case TelnetStateSubnegotiation1:
- case TelnetStateSubnegotiation2:
- TelnetHandleSubnegotiation(Context, Character);
- break;
- default:
- assert(FALSE);
- return EINVAL;
- }
- }
- if (Context->State != TelnetStateNormal) {
- if (Context->IacSize != 0) {
- TelnetFlushIacs(Context);
- }
- if (Context->State == TelnetStateCopy) {
- Context->State = TelnetStateNormal;
- }
- Size = CopyStart;
- }
- if (Size != 0) {
- return TelnetWrite(STDOUT_FILENO, Context->Buffer, Size);
- }
- return 0;
- }
- INT
- TelnetWrite (
- int Descriptor,
- void *Buffer,
- size_t Size
- )
- /*++
- Routine Description:
- This routine writes the given data out to the given file descriptor in full.
- Arguments:
- Descriptor - Supplies the descriptor to write to.
- Buffer - Supplies a pointer to the buffer to write.
- Size - Supplies the number of bytes to write.
- Return Value:
- Returns an integer exit code. 0 for success, nonzero otherwise.
- --*/
- {
- INT Status;
- ssize_t Written;
- while (Size != 0) {
- Written = write(Descriptor, Buffer, Size);
- if (Written <= 0) {
- if (errno == EINTR) {
- continue;
- }
- Status = errno;
- SwPrintError(Status, NULL, "Failed to write");
- return Status;
- }
- Buffer += Written;
- Size -= Written;
- }
- return 0;
- }
- INT
- TelnetEscape (
- PTELNET_CONTEXT Context
- )
- /*++
- Routine Description:
- This routine handles local telnet interaction.
- Arguments:
- Context - Supplies a pointer to the application context.
- Return Value:
- 0 on success.
- Returns an error number on failure.
- --*/
- {
- CHAR Character;
- INT Status;
- if (Context->Sigint != FALSE) {
- SwSetRawInputMode(NULL, NULL);
- }
- printf("\nConsole escape:\n"
- " l - Set line mode.\n"
- " c - Set character mode.\n"
- " z - Suspend telnet.\n",
- " e - Exit telnet.\n\n"
- "telnet> ");
- if (read(STDIN_FILENO, &Character, 1) <= 0) {
- Status = errno;
- SwPrintError(Status, NULL, "Failed to read");
- return Status;
- }
- switch (Character) {
- case 'l':
- TelnetSendDoLineMode(Context);
- goto TelnetEscapeEnd;
- case 'c':
- TelnetSendWillCharMode(Context);
- goto TelnetEscapeEnd;
- case 'z':
- SwRestoreInputMode();
- kill(0, SIGTSTP);
- SwSetRawInputMode(NULL, NULL);
- break;
- case 'e':
- Context->Exit = TRUE;
- goto TelnetEscapeEnd;
- }
- printf("Continuing...\n");
- if (Context->Sigint != FALSE) {
- SwRestoreInputMode();
- }
- TelnetEscapeEnd:
- Context->Sigint = FALSE;
- return 0;
- }
- VOID
- TelnetHandleOption (
- PTELNET_CONTEXT Context,
- CHAR Option
- )
- /*++
- Routine Description:
- This routine handles an incoming telnet option.
- Arguments:
- Context - Supplies a pointer to the application context.
- Option - Supplies the option to handle.
- Return Value:
- None.
- --*/
- {
- switch (Option) {
- case TELOPT_ECHO:
- TelnetHandleEchoOption(Context);
- break;
- case TELOPT_SGA:
- TelnetHandleSgaOption(Context);
- break;
- case TELOPT_TTYPE:
- TelnetHandleTTypeOption(Context);
- break;
- case TELOPT_NAWS:
- TelnetHandleNawsOption(Context);
- TelnetSendWindowSize(Context,
- Option,
- Context->WindowWidth,
- Context->WindowHeight);
- break;
- default:
- TelnetHandleUnsupportedOption(Context, Option);
- break;
- }
- return;
- }
- VOID
- TelnetHandleSubnegotiation (
- PTELNET_CONTEXT Context,
- CHAR Option
- )
- /*++
- Routine Description:
- This routine handles a telnet subnegotiation.
- Arguments:
- Context - Supplies a pointer to the application context.
- Option - Supplies the subnegotiation option.
- Return Value:
- None.
- --*/
- {
- switch (Context->State) {
- case TelnetStateSubnegotiation1:
- if (Option == IAC) {
- Context->State = TelnetStateSubnegotiation2;
- break;
- }
- if ((Option == TELOPT_TTYPE) && (Context->TerminalType != NULL)) {
- TelnetSendSuboptIac(Context,
- TELOPT_TTYPE,
- Context->TerminalType);
- }
- break;
- case TelnetStateSubnegotiation2:
- if (Option == SE) {
- Context->State = TelnetStateCopy;
- break;
- }
- Context->State = TelnetStateSubnegotiation1;
- break;
- default:
- assert(FALSE);
- break;
- }
- return;
- }
- VOID
- TelnetHandleEchoOption (
- PTELNET_CONTEXT Context
- )
- /*++
- Routine Description:
- This routine handles an incoming telnet echo option.
- Arguments:
- Context - Supplies a pointer to the application context.
- Return Value:
- None.
- --*/
- {
- //
- // If the server wants the client to echo, refuse to do it.
- //
- if (Context->Wish == DO) {
- TelnetAddIacWish(Context, WONT, TELOPT_ECHO);
- return;
- }
- if (Context->Wish == DONT) {
- return;
- }
- //
- // Do nothing if they already agree with what the client is doing.
- //
- if ((Context->Flags & TELNET_FLAG_ECHO) != 0) {
- if (Context->Wish == WILL) {
- return;
- }
- } else if (Context->Wish == WONT) {
- return;
- }
- if (Context->CharacterMode != TelnetCharacterModeOff) {
- Context->Flags ^= TELNET_FLAG_ECHO;
- }
- if ((Context->Flags & TELNET_FLAG_ECHO) != 0) {
- TelnetAddIacWish(Context, DO, TELOPT_ECHO);
- } else {
- TelnetAddIacWish(Context, DONT, TELOPT_ECHO);
- }
- TelnetSetConsoleMode(Context);
- printf("\n");
- return;
- }
- VOID
- TelnetHandleSgaOption (
- PTELNET_CONTEXT Context
- )
- /*++
- Routine Description:
- This routine handles an incoming telnet Suppress Go Ahead option.
- Arguments:
- Context - Supplies a pointer to the application context.
- Return Value:
- None.
- --*/
- {
- //
- // Do nothing if it already agrees.
- //
- if ((Context->Flags & TELNET_FLAG_SUPPRESS_GO_AHEAD) != 0) {
- if (Context->Wish == WILL) {
- return;
- }
- } else if (Context->Wish == WONT) {
- return;
- }
- Context->Flags ^= TELNET_FLAG_SUPPRESS_GO_AHEAD;
- if ((Context->Flags & TELNET_FLAG_SUPPRESS_GO_AHEAD) != 0) {
- TelnetAddIacWish(Context, DO, TELOPT_SGA);
- } else {
- TelnetAddIacWish(Context, DONT, TELOPT_SGA);
- }
- return;
- }
- VOID
- TelnetHandleTTypeOption (
- PTELNET_CONTEXT Context
- )
- /*++
- Routine Description:
- This routine handles an incoming telnet terminal type option.
- Arguments:
- Context - Supplies a pointer to the application context.
- Return Value:
- None.
- --*/
- {
- if (Context->TerminalType != NULL) {
- TelnetAddIacWish(Context, WILL, TELOPT_TTYPE);
- } else {
- TelnetAddIacWish(Context, WONT, TELOPT_TTYPE);
- }
- return;
- }
- VOID
- TelnetHandleNawsOption (
- PTELNET_CONTEXT Context
- )
- /*++
- Routine Description:
- This routine handles an incoming telnet window size option.
- Arguments:
- Context - Supplies a pointer to the application context.
- Return Value:
- None.
- --*/
- {
- TelnetAddIacWish(Context, WILL, TELOPT_NAWS);
- return;
- }
- VOID
- TelnetHandleUnsupportedOption (
- PTELNET_CONTEXT Context,
- CHAR Option
- )
- /*++
- Routine Description:
- This routine handles an incoming telnet option that this client does not
- recognize.
- Arguments:
- Context - Supplies a pointer to the application context.
- Option - Supplies the unsupported option.
- Return Value:
- None.
- --*/
- {
- if (Context->Wish == WILL) {
- TelnetAddIacWish(Context, WONT, Option);
- } else {
- TelnetAddIacWish(Context, DONT, Option);
- }
- return;
- }
- VOID
- TelnetSendWindowSize (
- PTELNET_CONTEXT Context,
- UCHAR Option,
- INT Width,
- INT Height
- )
- /*++
- Routine Description:
- This routine sends a window size IAC to the server.
- Arguments:
- Context - Supplies a pointer to the application context.
- Option - Supplies the option byte received.
- Width - Supplies the window width in characters.
- Height - Supplies the window height in characters.
- Return Value:
- None.
- --*/
- {
- if (Context->IacSize + 9 > TELNET_IAC_BUFFER_SIZE) {
- TelnetFlushIacs(Context);
- }
- TelnetAddIac(Context, IAC);
- TelnetAddIac(Context, SB);
- TelnetAddIac(Context, Option);
- TelnetAddIac(Context, (Width >> BITS_PER_BYTE) & 0xFF);
- TelnetAddIac(Context, Width & 0xFF);
- TelnetAddIac(Context, (Height >> BITS_PER_BYTE) & 0xFF);
- TelnetAddIac(Context, Height & 0xFF);
- TelnetAddIac(Context, IAC);
- TelnetAddIac(Context, SE);
- return;
- }
- VOID
- TelnetSendSuboptIac (
- PTELNET_CONTEXT Context,
- UCHAR Option,
- PSTR String
- )
- /*++
- Routine Description:
- This routine sends a suboption IAC control sequence.
- Arguments:
- Context - Supplies a pointer to the application context.
- Option - Supplies the option byte to send.
- String - Supplies a pointer to a null-terminated string to send as the
- argument.
- Return Value:
- None.
- --*/
- {
- ULONG Length;
- Length = strlen(String) + 6;
- if (Context->IacSize + Length > TELNET_IAC_BUFFER_SIZE) {
- TelnetFlushIacs(Context);
- }
- TelnetAddIac(Context, IAC);
- TelnetAddIac(Context, SB);
- TelnetAddIac(Context, Option);
- TelnetAddIac(Context, 0);
- while (*String != '\0') {
- TelnetAddIac(Context, *String);
- String += 1;
- }
- TelnetAddIac(Context, IAC);
- TelnetAddIac(Context, SE);
- return;
- }
- VOID
- TelnetSendDoLineMode (
- PTELNET_CONTEXT Context
- )
- /*++
- Routine Description:
- This routine handles transitioning the client to line mode.
- Arguments:
- Context - Supplies a pointer to the application context.
- Return Value:
- None.
- --*/
- {
- Context->CharacterMode = TelnetCharacterModeTry;
- Context->Flags &= ~(TELNET_FLAG_ECHO | TELNET_FLAG_SUPPRESS_GO_AHEAD);
- TelnetSetConsoleMode(Context);
- TelnetAddIacWish(Context, DONT, TELOPT_ECHO);
- TelnetAddIacWish(Context, DONT, TELOPT_SGA);
- TelnetFlushIacs(Context);
- return;
- }
- VOID
- TelnetSendWillCharMode (
- PTELNET_CONTEXT Context
- )
- /*++
- Routine Description:
- This routine handles transitioning the client to character mode.
- Arguments:
- Context - Supplies a pointer to the application context.
- Return Value:
- None.
- --*/
- {
- Context->CharacterMode = TelnetCharacterModeTry;
- Context->Flags |= TELNET_FLAG_ECHO | TELNET_FLAG_SUPPRESS_GO_AHEAD;
- TelnetSetConsoleMode(Context);
- TelnetAddIacWish(Context, DO, TELOPT_ECHO);
- TelnetAddIacWish(Context, DO, TELOPT_SGA);
- TelnetFlushIacs(Context);
- return;
- }
- VOID
- TelnetSetConsoleMode (
- PTELNET_CONTEXT Context
- )
- /*++
- Routine Description:
- This routine sets the current console mode to either raw mode or cooked
- mode depending on the flags in the context.
- Arguments:
- Context - Supplies a pointer to the application context.
- Return Value:
- None.
- --*/
- {
- if ((Context->Flags & TELNET_FLAG_ECHO) != 0) {
- if (Context->CharacterMode == TelnetCharacterModeTry) {
- Context->CharacterMode = TelnetCharacterModeOn;
- printf(TELNET_ENTERING_MODE_FORMAT, "character", "^]");
- SwSetRawInputMode(NULL, NULL);
- }
- } else {
- if (Context->CharacterMode != TelnetCharacterModeOff) {
- Context->CharacterMode = TelnetCharacterModeOff;
- printf(TELNET_ENTERING_MODE_FORMAT, "line", "^C");
- SwRestoreInputMode();
- }
- }
- return;
- }
- VOID
- TelnetAddIacWish (
- PTELNET_CONTEXT Context,
- UCHAR Wish,
- UCHAR Option
- )
- /*++
- Routine Description:
- This routine sends an IAC DO/DONT/WILL/WONT sequence to the outgoing IAC
- buffer.
- Arguments:
- Context - Supplies a pointer to the application context.
- Wish - Supplies the wish, which should be DO, DONT, WILL, or WONT (defined
- in telnet.h).
- Option - Supplies the option to wish for or against.
- Return Value:
- None.
- --*/
- {
- if (Context->IacSize + 3 > TELNET_IAC_BUFFER_SIZE) {
- TelnetFlushIacs(Context);
- }
- TelnetAddIac(Context, IAC);
- TelnetAddIac(Context, Wish);
- TelnetAddIac(Context, Option);
- return;
- }
- VOID
- TelnetAddIac (
- PTELNET_CONTEXT Context,
- UCHAR Character
- )
- /*++
- Routine Description:
- This routine adds a character to the IAC buffer.
- Arguments:
- Context - Supplies a pointer to the application context.
- Character - Supplies the character to add.
- Return Value:
- None.
- --*/
- {
- if (Context->IacSize >= TELNET_IAC_BUFFER_SIZE) {
- TelnetFlushIacs(Context);
- }
- assert(Context->IacSize < TELNET_IAC_BUFFER_SIZE);
- Context->IacBuffer[Context->IacSize] = Character;
- Context->IacSize += 1;
- return;
- }
- VOID
- TelnetFlushIacs (
- PTELNET_CONTEXT Context
- )
- /*++
- Routine Description:
- This routine writes the IAC buffer out to the socket.
- Arguments:
- Context - Supplies a pointer to the application context.
- Return Value:
- None.
- --*/
- {
- TelnetWrite(Context->Socket, Context->IacBuffer, Context->IacSize);
- Context->IacSize = 0;
- return;
- }
|