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  };
167 
170  {
171  public:
173  enum class SmtpStatusCode : int
174  {
176  SYSTEM_STATUS = 211,
178  HELP_MESSAGE = 214,
180  SERVICE_READY = 220,
182  SERVICE_CLOSE = 221,
184  AUTH_SUCCESS = 235,
186  COMPLETED = 250,
188  WILL_FORWARD = 251,
190  CANNOT_VERIFY = 252,
192  AUTH_INPUT = 334,
194  MAIL_INPUT = 354,
196  SERVICE_UNAVAILABLE = 421,
198  PASS_NEEDED = 432,
202  ABORT_LOCAL_ERROR = 451,
204  INSUFFICIENT_STORAGE = 452,
206  TEMP_AUTH_FAILED = 454,
210  CMD_NOT_RECOGNIZED = 500,
212  SYNTAX_ERROR_PARAM = 501,
214  CMD_NOT_IMPLEMENTED = 502,
216  CMD_BAD_SEQUENCE = 503,
218  PARAM_NOT_IMPLEMENTED = 504,
220  MAIL_NOT_ACCEPTED = 521,
222  ENCRYPT_NEED = 523,
224  AUTH_REQUIRED = 530,
226  AUTH_TOO_WEAK = 534,
228  AUTH_CRED_INVALID = 535,
230  ENCRYPT_REQUIRED = 538,
232  MAILBOX_UNAVAILABLE = 550,
234  USER_NOT_LOCAL = 551,
236  EXCEED_STORAGE = 552,
238  NAME_NOT_ALLOWED = 553,
240  TRANSACTION_FAIL = 554,
242  DOMAIN_NOT_ACCEPT = 556
243  };
244 
250  SmtpResponseLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
251  : SmtpLayer(data, dataLen, prevLayer, packet) {};
252 
256  explicit SmtpResponseLayer(const SmtpStatusCode& code, const std::string& option = "")
257  : SmtpLayer(std::to_string(int(code)), option) {};
258 
263 
267 
270  std::string getStatusCodeString() const;
271 
275  bool setStatusOption(const std::string& value);
276 
280  std::string getStatusOption(bool removeEscapeCharacters = true) const;
281 
285  static std::string getStatusCodeAsString(SmtpStatusCode code);
286 
287  // overridden methods
288 
290  std::string toString() const override;
291  };
292 } // namespace pcpp
293 
294 #endif // PACKETPP_SMTP_LAYER
Definition: Layer.h:60
Definition: Packet.h:22
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
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:170
SmtpResponseLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: SmtpLayer.h:250
static std::string getStatusCodeAsString(SmtpStatusCode code)
bool setStatusOption(const std::string &value)
SmtpStatusCode
Enum for SMTP response codes.
Definition: SmtpLayer.h:174
@ 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:256
SmtpStatusCode getStatusCode() const
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
An enum representing OSI model layers.
Definition: ProtocolType.h:225
@ OsiModelApplicationLayer
Application layer (layer 7)
Definition: ProtocolType.h:239
const ProtocolType SMTP
SMTP protocol.
Definition: ProtocolType.h:209