PcapPlusPlus  Next
TelnetLayer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Layer.h"
4 
5 #include <algorithm>
6 #include <iterator>
7 
9 
12 namespace pcpp
13 {
15  class TelnetLayer : public Layer
16  {
17  private:
18  // Position iterator for next command
19  size_t lastPositionOffset;
20 
21  // Returns distance to next IAC
22  size_t distanceToNextIAC(uint8_t* startPos, size_t maxLength) const;
23  // Returns length of provided field
24  size_t getFieldLen(uint8_t* startPos, size_t maxLength) const;
25  // Get position of next data field
26  uint8_t* getNextDataField(uint8_t* pos, size_t len) const;
27  // Get position of next command field
28  uint8_t* getNextCommandField(uint8_t* pos, size_t len) const;
29  // Get options of provided field
30  int16_t getSubCommand(const uint8_t* pos, size_t len) const;
31  // Get data of provided field
32  uint8_t* getCommandData(uint8_t* pos, size_t& slen) const;
33 
34  public:
36  enum class TelnetCommand : int
37  {
40 
42  EndOfFile = 236,
44  Suspend,
46  Abort,
55  DataMark,
57  Break,
71  EraseLine,
73  GoAhead,
85  DoPerform,
92  };
93 
95  enum class TelnetOption : int
96  {
99 
101  TransmitBinary = 0,
103  Echo,
105  Reconnection,
111  Status,
113  TimingMark,
137  Logout,
139  ByteMacro,
143  SUPDUP,
145  SUPDUPOutput,
147  SendLocation,
149  TerminalType,
161  X3Pad,
169  Linemode,
181  TN3270E,
183  XAuth,
185  Charset,
195  Kermit,
197  SendURL,
199  ForwardX,
200 
202  TelOptPragmaLogon = 138,
207 
209  ExtendedOptions = 255
210  };
211 
217  TelnetLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
218  : Layer(data, dataLen, prevLayer, packet, Telnet)
219  {
220  lastPositionOffset = SIZE_MAX;
221  };
222 
226  std::string getDataAsString(bool removeEscapeCharacters = true) const;
227 
231 
236 
240 
245 
249 
254 
258  uint8_t* getOptionData(size_t& length);
259 
265  uint8_t* getOptionData(TelnetCommand command, size_t& length);
266 
270  static std::string getTelnetCommandAsString(TelnetCommand val);
271 
275  static std::string getTelnetOptionAsString(TelnetOption val);
276 
279  static bool isTelnetPort(uint16_t port)
280  {
281  return port == 23;
282  }
283 
288  static bool isDataValid(const uint8_t* data, size_t dataSize)
289  {
290  return data && dataSize;
291  }
292 
293  // overridden methods
294 
296  void parseNextLayer() override
297  {}
298 
300  size_t getHeaderLen() const override
301  {
302  return m_DataLen;
303  }
304 
306  void computeCalculateFields() override
307  {}
308 
311  {
313  }
314 
316  std::string toString() const override;
317 
322  {
324  static std::vector<FieldDescriptor> all()
325  {
326  auto result = Layer::SerializedFields::all();
327  std::initializer_list<FieldDescriptor> extra{ Data, Commands };
328  std::copy(extra.begin(), extra.end(), std::back_inserter(result));
329  return result;
330  }
331 
334  struct CommandObject : ObjectFieldDescriptor<CommandObject>
335  {
337 
339  static std::vector<FieldDescriptor> all()
340  {
341  return { Command, CommandName, Option, OptionName };
342  }
343 
345  static const FieldDescriptor Command;
346 
349 
352  static const FieldDescriptor Option;
353 
357  };
358 
361  static const FieldDescriptor Data;
362 
364  static const FieldDescriptor Commands;
365  };
366 
367  protected:
368  void serializeLayer(ObjectScope& serializer) const override;
369  };
370 
371 } // namespace pcpp
Definition: Layer.h:117
Definition: Serializers.h:230
Definition: Packet.h:49
Class for representing the Telnet Layer.
Definition: TelnetLayer.h:16
void computeCalculateFields() override
Does nothing for this layer.
Definition: TelnetLayer.h:306
TelnetOption getOption(TelnetCommand command)
static std::string getTelnetOptionAsString(TelnetOption val)
static bool isDataValid(const uint8_t *data, size_t dataSize)
Definition: TelnetLayer.h:288
uint8_t * getOptionData(size_t &length)
TelnetCommand getNextCommand()
static std::string getTelnetCommandAsString(TelnetCommand val)
std::string toString() const override
OsiModelLayer getOsiModelLayer() const override
Definition: TelnetLayer.h:310
void serializeLayer(ObjectScope &serializer) const override
std::string getDataAsString(bool removeEscapeCharacters=true) const
static bool isTelnetPort(uint16_t port)
Definition: TelnetLayer.h:279
size_t getTotalNumberOfCommands()
TelnetLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: TelnetLayer.h:217
TelnetCommand
Telnet Command Indicator.
Definition: TelnetLayer.h:37
@ TelnetCommandEndOfPacket
Indicator to parser reached end of packet.
@ NoOperation
Null command; does nothing.
@ InterruptProcess
Tells the recipient to interrupt, abort, suspend or terminate the process currently in use.
@ DataMark
Used to mark the end of a sequence of data that the recipient should scan for urgent Telnet commands.
@ GoAhead
Used in Telnet half-duplex mode to signal the other device that it may transmit.
@ Suspend
Suspend current process.
@ Break
Represents the pressing of the “break” or “attention” key on the terminal.
uint8_t * getOptionData(TelnetCommand command, size_t &length)
TelnetOption
Telnet Options.
Definition: TelnetLayer.h:96
@ Telnet3270Regime
Telnet 3270 Regime RFC1041 https://www.iana.org/go/rfc1041.
@ ExtendedOptions
Extended option list.
@ SuppressGoAhead
Suppress Go Ahead RFC858 https://www.iana.org/go/rfc858.
@ Logout
Logout RFC727 https://www.iana.org/go/rfc727.
@ NewEnvironmentOption
New Environment Option RFC1572 https://www.iana.org/go/rfc1572.
@ ExtendedASCII
Extended ASCII RFC698 https://www.iana.org/go/rfc698.
@ OutputHorizontalTabDisposition
NAOHTD, Negotiate About Output Horizontal Tab Disposition RFC654 https://www.iana....
@ XDisplayLocation
X Display Location RFC1096 https://www.iana.org/go/rfc1096.
@ OutputMarking
OUTMRK, Output Marking RFC933 https://www.iana.org/go/rfc933.
@ RemoteFlowControl
Remote Flow Control RFC1372 https://www.iana.org/go/rfc1372.
@ TelOptPragmaHeartbeat
Telnet Option Pragma Heartbeat.
@ ByteMacro
BM, Byte Macro RFC735 https://www.iana.org/go/rfc735.
@ Linemode
Line Mode RFC1184 https://www.iana.org/go/rfc1184.
@ TelnetStartTLS
Telnet Start TLS.
@ OutputVerticalTabStops
NAOVTS, Negotiate About Vertical Tabstops RFC656 https://www.iana.org/go/rfc656.
@ TACACSUserIdentification
TUID, TACACS User Identification RFC927 https://www.iana.org/go/rfc927.
@ EncryptionOption
Encryption Option RFC2946 https://www.iana.org/go/rfc2946.
@ EnvironmentOption
Environment Option RFC1408 https://www.iana.org/go/rfc1408.
@ OutputPageSize
Output Page Size.
@ RemoteControlledTransAndEcho
RCTE, Remote Controlled Transmission and Echo RFC726 https://www.iana.org/go/rfc726.
@ TerminalType
Terminal Type RFC1091 https://www.iana.org/go/rfc1091.
@ TelOptSSPILogon
Telnet Option SSPI Logon.
@ OutputVerticalTabDisposition
NAOVTD, Negotiate About Output Vertcial Tab Disposition RFC657 https://www.iana.org/go/rfc657.
@ TerminalLocationNumber
TTYLOC, Terminal Location Number RFC946 https://www.iana.org/go/rfc946.
@ EndOfRecordOption
End of record RFC885 https://www.iana.org/go/rfc885.
@ X3Pad
X.3 PAD RFC1053 https://www.iana.org/go/rfc1053.
@ Kermit
Kermit RFC2840 https://www.iana.org/go/rfc2840.
@ AuthenticationOption
Authentication Option RFC2941 https://www.iana.org/go/rfc2941.
@ TelOptPragmaLogon
Telnet Option Pragma Logon.
@ OutputHorizontalTabStops
NAOHTS, Negotiate About Output Horizontal Tabstops RFC653 https://www.iana.org/go/rfc653.
@ TN3270E
TN3270E RFC2355 https://www.iana.org/go/rfc2355.
@ TelnetRemoteSerialPort
RSP, Telnet Remote Serial Port.
@ TelnetOptionNoOption
Internal return for no option detected.
@ OutputLineWidth
Output Line Width.
@ DataEntryTerminal
Data Entry Terminal RFC1043 - RFC732 https://www.iana.org/go/rfc1043 https://www.iana....
@ SUPDUP
SUPDUP RFC736 - RFC734 https://www.iana.org/go/rfc736 https://www.iana.org/go/rfc734.
@ OutputCarriageReturnDisposition
NAOCRD, Negotiate About Output Carriage-Return Disposition RFC652 https://www.iana....
@ TransmitBinary
Binary Transmission RFC856 https://www.iana.org/go/rfc856.
@ SUPDUPOutput
SUPDUP Output RFC749 https://www.iana.org/go/rfc749.
@ TelnetSuppressLocalEcho
Telnet Suppress Local Echo.
@ Echo
Echo RFC857 https://www.iana.org/go/rfc857.
@ XAuth
X Server Authentication.
@ SendLocation
Send Location RFC779 https://www.iana.org/go/rfc779.
@ OutputFormfeedDisposition
NAOFFD, Negotiate About Output Formfeed Disposition RFC655 https://www.iana.org/go/rfc655.
@ TerminalSpeed
Terminal Speed RFC1079 https://www.iana.org/go/rfc1079.
@ ComPortControlOption
Com Port Control Option RFC2217 https://www.iana.org/go/rfc2217.
@ Status
Status RFC859 https://www.iana.org/go/rfc859.
@ ApproxMsgSizeNegotiation
Negotiate approximate message size.
@ NegotiateAboutWindowSize
NAWS, Negotiate About Window Size RFC1073 https://www.iana.org/go/rfc1073.
@ OutputLinefeedDisposition
NAOLFD, Negotiate About Output Linefeed Disposition RFC658 https://www.iana.org/go/rfc658.
@ Charset
Charset RFC2066 https://www.iana.org/go/rfc2066.
@ TimingMark
Timing Mark RFC860 https://www.iana.org/go/rfc860.
void parseNextLayer() override
Parses the next layer. Telnet is the always last so does nothing for this layer.
Definition: TelnetLayer.h:296
TelnetOption getOption()
TelnetCommand getFirstCommand()
size_t getNumberOfCommands(TelnetCommand command)
size_t getHeaderLen() const override
Definition: TelnetLayer.h:300
const ProtocolType Telnet
Telnet Protocol.
Definition: ProtocolType.h:189
The main namespace for the PcapPlusPlus lib.
Definition: AssertionUtils.h:19
OsiModelLayer
An enum representing OSI model layers.
Definition: ProtocolType.h:430
@ OsiModelApplicationLayer
Application layer (layer 7)
Definition: ProtocolType.h:446
Definition: Serializers.h:18
Definition: Layer.h:212
static std::vector< FieldDescriptor > all()
Definition: Layer.h:214
Definition: Serializers.h:49
ObjectFieldDescriptor(uint16_t fieldId, std::string fieldName)
Definition: Serializers.h:53
static const FieldDescriptor Option
Telnet option code, written as a number (see TelnetOption). Omitted when the command has no option.
Definition: TelnetLayer.h:352
static std::vector< FieldDescriptor > all()
Definition: TelnetLayer.h:339
static const FieldDescriptor OptionName
Telnet option as a readable string (see getTelnetOptionAsString()). Omitted when the command has no o...
Definition: TelnetLayer.h:356
static const FieldDescriptor CommandName
Telnet command as a readable string (see getTelnetCommandAsString())
Definition: TelnetLayer.h:348
static const FieldDescriptor Command
Telnet command code, written as a number (see TelnetCommand)
Definition: TelnetLayer.h:345
Definition: TelnetLayer.h:322
static const FieldDescriptor Commands
Array of Telnet commands in packet order. Elements are described by CommandObject.
Definition: TelnetLayer.h:364
static std::vector< FieldDescriptor > all()
Definition: TelnetLayer.h:324
static const FieldDescriptor Data
Telnet user data as a readable string (see getDataAsString()). Empty for control-only packets.
Definition: TelnetLayer.h:361