PcapPlusPlus  Next
SmtpLayer.h
Go to the documentation of this file.
1 #ifndef PACKETPP_SMTP_LAYER
2 #define PACKETPP_SMTP_LAYER
3 
4 #include "PayloadLayer.h"
6 
8 
11 namespace pcpp
12 {
15  {
16  protected:
17  SmtpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
18  : SingleCommandTextProtocol(data, dataLen, prevLayer, packet, SMTP) {};
19 
20  SmtpLayer(const std::string& command, const std::string& option)
21  : SingleCommandTextProtocol(command, option, SMTP) {};
22 
23  public:
27  static bool isSmtpPort(uint16_t port)
28  {
29  return port == 25 || port == 587;
30  }
31 
32  // overridden methods
33 
35  void parseNextLayer() override
36  {}
37 
39  size_t getHeaderLen() const override
40  {
41  return m_DataLen;
42  }
43 
45  void computeCalculateFields() override
46  {}
47 
50  {
52  }
53  };
54 
56  class SmtpRequestLayer : public SmtpLayer
57  {
58  public:
60  enum class SmtpCommand : uint64_t
61  {
63  UNK,
65  DATA = ('D') | ('A' << 8) | ('T' << 16) | ('A' << 24),
67  EHLO = ('E') | ('H' << 8) | ('L' << 16) | ('O' << 24),
69  EXPN = ('E') | ('X' << 8) | ('P' << 16) | ('N' << 24),
71  HELO = ('H') | ('E' << 8) | ('L' << 16) | ('O' << 24),
73  HELP = ('H') | ('E' << 8) | ('L' << 16) | ('P' << 24),
75  MAIL = ('M') | ('A' << 8) | ('I' << 16) | ('L' << 24),
77  NOOP = ('N') | ('O' << 8) | ('O' << 16) | ('P' << 24),
79  QUIT = ('Q') | ('U' << 8) | ('I' << 16) | ('T' << 24),
81  RCPT = ('R') | ('C' << 8) | ('P' << 16) | ('T' << 24),
83  RSET = ('R') | ('S' << 8) | ('E' << 16) | ('T' << 24),
85  VRFY = ('V') | ('R' << 8) | ('F' << 16) | ('Y' << 24),
87  STARTTLS = (('S') | ('T' << 8) | ('A' << 16) | ('R' << 24) |
88  static_cast<uint64_t>(('T') | ('T' << 8) | ('L' << 16) | ('S' << 24)) << 32),
90  TURN = ('T') | ('U' << 8) | ('R' << 16) | ('N' << 24),
92  SEND = ('S') | ('E' << 8) | ('N' << 16) | ('D' << 24),
94  SOML = ('S') | ('O' << 8) | ('M' << 16) | ('L' << 24),
96  SAML = ('S') | ('A' << 8) | ('M' << 16) | ('L' << 24),
98  AUTH = ('A') | ('U' << 8) | ('T' << 16) | ('H' << 24),
100  ATRN = ('A') | ('T' << 8) | ('R' << 16) | ('N' << 24),
102  BDAT = ('B') | ('D' << 8) | ('A' << 16) | ('T' << 24),
104  ETRN = ('E') | ('T' << 8) | ('R' << 16) | ('N' << 24),
106  XADR = ('X') | ('A' << 8) | ('D' << 16) | ('R' << 24),
108  XCIR = ('X') | ('C' << 8) | ('I' << 16) | ('R' << 24),
110  XSTA = ('X') | ('S' << 8) | ('T' << 16) | ('A' << 24),
112  XGEN = ('X') | ('G' << 8) | ('E' << 16) | ('N' << 24)
113  };
114 
120  SmtpRequestLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
121  : SmtpLayer(data, dataLen, prevLayer, packet) {};
122 
126  explicit SmtpRequestLayer(const SmtpCommand& command, const std::string& option = "")
127  : SmtpLayer(getCommandAsString(command), option) {};
128 
133 
137 
140  std::string getCommandString() const;
141 
145  bool setCommandOption(const std::string& value);
146 
150  std::string getCommandOption(bool removeEscapeCharacters = true) const;
151 
155  static std::string getCommandInfo(SmtpCommand code);
156 
160  static std::string getCommandAsString(SmtpCommand code);
161 
162  // overridden methods
163 
165  std::string toString() const override;
166 
171  {
173  static std::vector<FieldDescriptor> all()
174  {
175  auto result = Layer::SerializedFields::all();
176  std::initializer_list<FieldDescriptor> extra{ Command, CommandOption };
177  std::copy(extra.begin(), extra.end(), std::back_inserter(result));
178  return result;
179  }
180 
183  static const FieldDescriptor Command;
184 
188  };
189 
190  protected:
191  void serializeLayer(ObjectScope& serializer) const override;
192  };
193 
196  {
197  public:
199  enum class SmtpStatusCode : int
200  {
202  SYSTEM_STATUS = 211,
204  HELP_MESSAGE = 214,
206  SERVICE_READY = 220,
208  SERVICE_CLOSE = 221,
210  AUTH_SUCCESS = 235,
212  COMPLETED = 250,
214  WILL_FORWARD = 251,
216  CANNOT_VERIFY = 252,
218  AUTH_INPUT = 334,
220  MAIL_INPUT = 354,
222  SERVICE_UNAVAILABLE = 421,
224  PASS_NEEDED = 432,
228  ABORT_LOCAL_ERROR = 451,
230  INSUFFICIENT_STORAGE = 452,
232  TEMP_AUTH_FAILED = 454,
236  CMD_NOT_RECOGNIZED = 500,
238  SYNTAX_ERROR_PARAM = 501,
240  CMD_NOT_IMPLEMENTED = 502,
242  CMD_BAD_SEQUENCE = 503,
244  PARAM_NOT_IMPLEMENTED = 504,
246  MAIL_NOT_ACCEPTED = 521,
248  ENCRYPT_NEED = 523,
250  AUTH_REQUIRED = 530,
252  AUTH_TOO_WEAK = 534,
254  AUTH_CRED_INVALID = 535,
256  ENCRYPT_REQUIRED = 538,
258  MAILBOX_UNAVAILABLE = 550,
260  USER_NOT_LOCAL = 551,
262  EXCEED_STORAGE = 552,
264  NAME_NOT_ALLOWED = 553,
266  TRANSACTION_FAIL = 554,
268  DOMAIN_NOT_ACCEPT = 556
269  };
270 
276  SmtpResponseLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
277  : SmtpLayer(data, dataLen, prevLayer, packet) {};
278 
282  explicit SmtpResponseLayer(const SmtpStatusCode& code, const std::string& option = "")
283  : SmtpLayer(std::to_string(int(code)), option) {};
284 
289 
293 
296  std::string getStatusCodeString() const;
297 
301  bool setStatusOption(const std::string& value);
302 
306  std::string getStatusOption(bool removeEscapeCharacters = true) const;
307 
311  static std::string getStatusCodeAsString(SmtpStatusCode code);
312 
313  // overridden methods
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{ StatusCode, StatusCodeDescription, StatusOption,
328  MultiLine };
329  std::copy(extra.begin(), extra.end(), std::back_inserter(result));
330  return result;
331  }
332 
335 
339 
343 
346  };
347 
348  protected:
349  void serializeLayer(ObjectScope& serializer) const override;
350  };
351 } // namespace pcpp
352 
353 #endif // PACKETPP_SMTP_LAYER
Definition: Layer.h:117
Definition: Serializers.h:230
Definition: Packet.h:49
Class for single command text based protocol (FTP, SMTP) messages.
Definition: SingleCommandTextProtocol.h:14
Class for general SMTP message.
Definition: SmtpLayer.h:15
void parseNextLayer() override
SMTP is the always last so does nothing for this layer.
Definition: SmtpLayer.h:35
static bool isSmtpPort(uint16_t port)
Definition: SmtpLayer.h:27
size_t getHeaderLen() const override
Definition: SmtpLayer.h:39
void computeCalculateFields() override
Does nothing for this layer.
Definition: SmtpLayer.h:45
OsiModelLayer getOsiModelLayer() const override
Definition: SmtpLayer.h:49
Class for representing the request messages of SMTP Layer.
Definition: SmtpLayer.h:57
void serializeLayer(ObjectScope &serializer) const override
std::string getCommandString() const
SmtpCommand
Enum for SMTP command codes.
Definition: SmtpLayer.h:61
@ STARTTLS
Start TLS handshake.
@ SAML
Send mail to terminal and mailbox.
@ TURN
Reverse the role of sender and receiver.
@ ETRN
Request to start SMTP queue processing.
@ ATRN
Reverse the role of sender and receiver.
@ SEND
Send mail to terminal.
@ EXPN
Expand the mailing list.
@ EHLO
Initiate conversation.
@ XGEN
Release status of whether a compiled configuration and character set are in use.
@ SOML
Send mail to terminal or to mailbox.
@ XADR
Release status of the channel.
@ XSTA
Release status of the number of messages in channel queues.
@ XCIR
Release status of the circuit checking facility.
@ AUTH
Authenticate client and server.
@ HELO
Initiate conversation.
static std::string getCommandAsString(SmtpCommand code)
bool setCommand(SmtpCommand code)
std::string toString() const override
SmtpRequestLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: SmtpLayer.h:120
SmtpCommand getCommand() const
static std::string getCommandInfo(SmtpCommand code)
std::string getCommandOption(bool removeEscapeCharacters=true) const
SmtpRequestLayer(const SmtpCommand &command, const std::string &option="")
Definition: SmtpLayer.h:126
bool setCommandOption(const std::string &value)
Class for representing the response messages of SMTP Layer.
Definition: SmtpLayer.h:196
SmtpResponseLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: SmtpLayer.h:276
static std::string getStatusCodeAsString(SmtpStatusCode code)
bool setStatusOption(const std::string &value)
SmtpStatusCode
Enum for SMTP response codes.
Definition: SmtpLayer.h:200
@ NAME_NOT_ALLOWED
Requested action not taken: mailbox name not allowed.
@ PARAM_NOT_ACCOMMODATED
Server unable to accommodate parameters.
@ INSUFFICIENT_STORAGE
Requested action not taken: insufficient system storage.
@ SERVICE_READY
<domain> Service ready
@ EXCEED_STORAGE
Requested mail action aborted: exceeded storage allocation.
@ SERVICE_UNAVAILABLE
<domain> Service not available, closing transmission channel
@ MAILBOX_UNAVAILABLE_TEMP
Requested mail action not taken: mailbox unavailable (mail busy or temporarily blocked)
@ SERVICE_CLOSE
<domain> Service closing transmission channel
@ ENCRYPT_REQUIRED
Encryption required for requested authentication mechanism.
@ AUTH_CRED_INVALID
Authentication credentials invalid.
@ SYSTEM_STATUS
System status, or system help reply.
@ ABORT_LOCAL_ERROR
Requested action aborted: local error in processing.
@ CMD_BAD_SEQUENCE
Bad sequence of commands.
@ CMD_NOT_IMPLEMENTED
Command not implemented.
@ AUTH_REQUIRED
Authentication required.
@ CMD_NOT_RECOGNIZED
Syntax error, command unrecognized.
@ USER_NOT_LOCAL
User not local; please try <forward-path>
@ COMPLETED
Requested mail action okay, completed.
@ SYNTAX_ERROR_PARAM
Syntax error in parameters or arguments.
@ PASS_NEEDED
A password transition is needed.
@ DOMAIN_NOT_ACCEPT
Domain does not accept mail.
@ MAIL_NOT_ACCEPTED
Server does not accept mail.
@ MAILBOX_UNAVAILABLE
Requested action not taken: mailbox unavailable.
@ PARAM_NOT_IMPLEMENTED
Command parameter not implemented.
@ TEMP_AUTH_FAILED
Temporary authentication failed.
@ AUTH_SUCCESS
Authentication successful.
@ MAIL_INPUT
Start mail input; end with <CRLF>.<CRLF>
@ WILL_FORWARD
User not local; will forward to <forward-path>
@ AUTH_TOO_WEAK
Authentication mechanism is too weak.
@ CANNOT_VERIFY
Cannot VRFY user, but will accept message and attempt delivery.
std::string toString() const override
bool setStatusCode(SmtpStatusCode code)
std::string getStatusCodeString() const
std::string getStatusOption(bool removeEscapeCharacters=true) const
SmtpResponseLayer(const SmtpStatusCode &code, const std::string &option="")
Definition: SmtpLayer.h:282
void serializeLayer(ObjectScope &serializer) const override
SmtpStatusCode getStatusCode() const
const ProtocolType SMTP
SMTP protocol.
Definition: ProtocolType.h:234
The main namespace for the PcapPlusPlus lib.
Definition: AssertionUtils.h:19
OsiModelLayer
An enum representing OSI model layers.
Definition: ProtocolType.h:425
@ OsiModelApplicationLayer
Application layer (layer 7)
Definition: ProtocolType.h:441
Definition: Serializers.h:18
Definition: Layer.h:212
static std::vector< FieldDescriptor > all()
Definition: Layer.h:214
Definition: SmtpLayer.h:171
static const FieldDescriptor Command
Field descriptor for the command, written as a string as it appears in the message (for example "EHLO...
Definition: SmtpLayer.h:183
static std::vector< FieldDescriptor > all()
Definition: SmtpLayer.h:173
static const FieldDescriptor CommandOption
Field descriptor for the command argument, written as a string with non-printable characters removed....
Definition: SmtpLayer.h:187
static std::vector< FieldDescriptor > all()
Definition: SmtpLayer.h:324
static const FieldDescriptor StatusCodeDescription
Field descriptor for a short description of the status code, written as a string (see getStatusCodeAs...
Definition: SmtpLayer.h:338
static const FieldDescriptor StatusOption
Field descriptor for the status argument, written as a string with non-printable characters removed....
Definition: SmtpLayer.h:342
static const FieldDescriptor MultiLine
Field descriptor for whether this is a multi-line reply, written as a boolean.
Definition: SmtpLayer.h:345
static const FieldDescriptor StatusCode
Field descriptor for the status code, written as a number (for example 220).
Definition: SmtpLayer.h:334