Coverage Report

Created: 2026-12-01 07:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/PcapPlusPlus/Packet++/src/SmtpLayer.cpp
Line
Count
Source
1
#define LOG_MODULE PacketLogModuleSmtpLayer
2
3
#include "SmtpLayer.h"
4
5
#include <algorithm>
6
7
namespace pcpp
8
{
9
  // ----------------- Class SmtpRequestLayer -----------------
10
  bool SmtpRequestLayer::setCommand(SmtpCommand code)
11
0
  {
12
0
    return setCommandInternal(getCommandAsString(code));
13
0
  }
14
15
  SmtpRequestLayer::SmtpCommand SmtpRequestLayer::getCommand() const
16
0
  {
17
0
    size_t val = 0;
18
0
    std::string field = getCommandString();
19
20
0
    for (size_t idx = 0; idx < std::min(field.size(), static_cast<size_t>(8)); ++idx)
21
0
    {
22
0
      val |= static_cast<size_t>(field.c_str()[idx]) << (idx * 8);
23
0
    }
24
25
0
    return static_cast<SmtpCommand>(val);
26
0
  }
27
28
  std::string SmtpRequestLayer::getCommandString() const
29
0
  {
30
0
    return getCommandInternal();
31
0
  }
32
33
  bool SmtpRequestLayer::setCommandOption(const std::string& value)
34
0
  {
35
0
    return setCommandOptionInternal(value);
36
0
  }
37
38
  std::string SmtpRequestLayer::getCommandOption(bool removeEscapeCharacters) const
39
0
  {
40
0
    std::string option = getCommandOptionInternal();
41
0
    if (!removeEscapeCharacters)
42
0
    {
43
0
      return option;
44
0
    }
45
46
0
    std::string optionWithEscapeChars;
47
0
    std::copy_if(option.begin(), option.end(), std::back_inserter(optionWithEscapeChars),
48
0
                 [](char ch) { return ch < 127 && ch > 31; });
49
50
0
    return optionWithEscapeChars;
51
0
  }
52
53
  std::string SmtpRequestLayer::getCommandInfo(SmtpCommand code)
54
0
  {
55
0
    switch (code)
56
0
    {
57
0
    case SmtpCommand::DATA:
58
0
      return "Starting mail body";
59
0
    case SmtpCommand::EHLO:
60
0
      return "Initiate conversation";
61
0
    case SmtpCommand::EXPN:
62
0
      return "Expand the mailing list";
63
0
    case SmtpCommand::HELO:
64
0
      return "Initiate conversation";
65
0
    case SmtpCommand::HELP:
66
0
      return "Ask information";
67
0
    case SmtpCommand::MAIL:
68
0
      return "Sender indication";
69
0
    case SmtpCommand::NOOP:
70
0
      return "No operation";
71
0
    case SmtpCommand::QUIT:
72
0
      return "Close conversation";
73
0
    case SmtpCommand::RCPT:
74
0
      return "Receiver indication";
75
0
    case SmtpCommand::RSET:
76
0
      return "Abort transaction";
77
0
    case SmtpCommand::VRFY:
78
0
      return "Identify user";
79
0
    case SmtpCommand::STARTTLS:
80
0
      return "Start TLS handshake";
81
0
    case SmtpCommand::TURN:
82
0
      return "Reverse the role of sender and receiver";
83
0
    case SmtpCommand::SEND:
84
0
      return "Send mail to terminal";
85
0
    case SmtpCommand::SOML:
86
0
      return "Send mail to terminal or to mailbox";
87
0
    case SmtpCommand::SAML:
88
0
      return "Send mail to terminal and mailbox";
89
0
    case SmtpCommand::AUTH:
90
0
      return "Authenticate client and server";
91
0
    case SmtpCommand::ATRN:
92
0
      return "Reverse the role of sender and receiver";
93
0
    case SmtpCommand::BDAT:
94
0
      return "Submit mail contents";
95
0
    case SmtpCommand::ETRN:
96
0
      return "Request to start SMTP queue processing";
97
0
    case SmtpCommand::XADR:
98
0
      return "Release status of the channel";
99
0
    case SmtpCommand::XCIR:
100
0
      return "Release status of the circuit checking facility";
101
0
    case SmtpCommand::XSTA:
102
0
      return "Release status of the number of messages in channel queues";
103
0
    case SmtpCommand::XGEN:
104
0
      return "Release status of whether a compiled configuration and character set are in use";
105
0
    default:
106
0
      return "Unknown command";
107
0
    }
108
0
  }
109
110
  std::string SmtpRequestLayer::getCommandAsString(SmtpCommand code)
111
0
  {
112
0
    std::stringstream oss;
113
0
    for (size_t idx = 0; idx < 8; ++idx)
114
0
    {
115
0
      char val = (uint64_t(code) >> (8 * idx)) & UINT8_MAX;
116
0
      if (val)  // Dont push if it is a null character
117
0
      {
118
0
        oss << val;
119
0
      }
120
0
    }
121
0
    return oss.str();
122
0
  }
123
124
  std::string SmtpRequestLayer::toString() const
125
0
  {
126
0
    return "SMTP request layer, command: " + getCommandInfo(getCommand());
127
0
  }
128
129
  const FieldDescriptor SmtpRequestLayer::SerializedFields::Command{ Layer::SerializedFields::MaxID + 1, "command" };
130
  const FieldDescriptor SmtpRequestLayer::SerializedFields::CommandOption{ Layer::SerializedFields::MaxID + 2,
131
                                                                         "commandOption" };
132
133
  void SmtpRequestLayer::serializeLayer(ObjectScope& serializer) const
134
0
  {
135
0
    serializer.writeField(SerializedFields::Command, getCommandString());
136
0
    serializer.writeField(SerializedFields::CommandOption, getCommandOption());
137
0
  }
138
139
  // ----------------- Class SmtpResponseLayer -----------------
140
  bool SmtpResponseLayer::setStatusCode(SmtpStatusCode code)
141
0
  {
142
0
    std::ostringstream oss;
143
0
    oss << int(code);
144
0
    return setCommandInternal(oss.str());
145
0
  }
146
147
  SmtpResponseLayer::SmtpStatusCode SmtpResponseLayer::getStatusCode() const
148
0
  {
149
0
    return static_cast<SmtpStatusCode>(atoi(getCommandInternal().c_str()));
150
0
  }
151
152
  std::string SmtpResponseLayer::getStatusCodeString() const
153
0
  {
154
0
    return getCommandInternal();
155
0
  }
156
157
  bool SmtpResponseLayer::setStatusOption(const std::string& value)
158
0
  {
159
0
    return setCommandOptionInternal(value);
160
0
  }
161
162
  std::string SmtpResponseLayer::getStatusOption(bool removeEscapeCharacters) const
163
0
  {
164
0
    std::string option = getCommandOptionInternal();
165
0
    if (!removeEscapeCharacters)
166
0
    {
167
0
      return option;
168
0
    }
169
170
0
    std::string optionWithEscapeChars;
171
0
    std::copy_if(option.begin(), option.end(), std::back_inserter(optionWithEscapeChars),
172
0
                 [](char ch) { return ch < 127 && ch > 31; });
173
174
0
    return optionWithEscapeChars;
175
0
  }
176
177
  std::string SmtpResponseLayer::getStatusCodeAsString(SmtpStatusCode code)
178
0
  {
179
0
    switch (code)
180
0
    {
181
0
    case SmtpStatusCode::SYSTEM_STATUS:
182
0
      return "System status, or system help reply";
183
0
    case SmtpStatusCode::HELP_MESSAGE:
184
0
      return "Help message";
185
0
    case SmtpStatusCode::SERVICE_READY:
186
0
      return "Service ready";
187
0
    case SmtpStatusCode::SERVICE_CLOSE:
188
0
      return "Service closing transmission channel";
189
0
    case SmtpStatusCode::AUTH_SUCCESS:
190
0
      return "Authentication successful";
191
0
    case SmtpStatusCode::COMPLETED:
192
0
      return "Requested mail action okay, completed";
193
0
    case SmtpStatusCode::WILL_FORWARD:
194
0
      return "User not local; will forward to <forward-path>";
195
0
    case SmtpStatusCode::CANNOT_VERIFY:
196
0
      return "Cannot VRFY user, but will accept message and attempt delivery";
197
0
    case SmtpStatusCode::AUTH_INPUT:
198
0
      return "AUTH input";
199
0
    case SmtpStatusCode::MAIL_INPUT:
200
0
      return "Start mail input; end with <CRLF>.<CRLF>";
201
0
    case SmtpStatusCode::SERVICE_UNAVAILABLE:
202
0
      return "Service not available, closing transmission channel";
203
0
    case SmtpStatusCode::PASS_NEEDED:
204
0
      return "A password transition is needed";
205
0
    case SmtpStatusCode::MAILBOX_UNAVAILABLE_TEMP:
206
0
      return "Requested mail action not taken: mailbox unavailable (mail busy or temporarily blocked)";
207
0
    case SmtpStatusCode::ABORT_LOCAL_ERROR:
208
0
      return "Requested action aborted: local error in processing";
209
0
    case SmtpStatusCode::INSUFFICIENT_STORAGE:
210
0
      return "Requested action not taken: insufficient system storage";
211
0
    case SmtpStatusCode::TEMP_AUTH_FAILED:
212
0
      return "Temporary authentication failed";
213
0
    case SmtpStatusCode::PARAM_NOT_ACCOMMODATED:
214
0
      return "Server unable to accommodate parameters";
215
0
    case SmtpStatusCode::CMD_NOT_RECOGNIZED:
216
0
      return "Syntax error, command unrecognized";
217
0
    case SmtpStatusCode::SYNTAX_ERROR_PARAM:
218
0
      return "Syntax error in parameters or arguments";
219
0
    case SmtpStatusCode::CMD_NOT_IMPLEMENTED:
220
0
      return "Command not implemented";
221
0
    case SmtpStatusCode::CMD_BAD_SEQUENCE:
222
0
      return "Bad sequence of commands";
223
0
    case SmtpStatusCode::PARAM_NOT_IMPLEMENTED:
224
0
      return "Command parameter not implemented";
225
0
    case SmtpStatusCode::MAIL_NOT_ACCEPTED:
226
0
      return "Server does not accept mail";
227
0
    case SmtpStatusCode::ENCRYPT_NEED:
228
0
      return "Encryption needed";
229
0
    case SmtpStatusCode::AUTH_REQUIRED:
230
0
      return "Authentication required";
231
0
    case SmtpStatusCode::AUTH_TOO_WEAK:
232
0
      return "Authentication mechanism is too weak";
233
0
    case SmtpStatusCode::AUTH_CRED_INVALID:
234
0
      return "Authentication credentials invalid";
235
0
    case SmtpStatusCode::ENCRYPT_REQUIRED:
236
0
      return "Encryption required for requested authentication mechanism";
237
0
    case SmtpStatusCode::MAILBOX_UNAVAILABLE:
238
0
      return "Requested action not taken: mailbox unavailable";
239
0
    case SmtpStatusCode::USER_NOT_LOCAL:
240
0
      return "User not local; please try <forward-path>";
241
0
    case SmtpStatusCode::EXCEED_STORAGE:
242
0
      return "Requested mail action aborted: exceeded storage allocation";
243
0
    case SmtpStatusCode::NAME_NOT_ALLOWED:
244
0
      return "Requested action not taken: mailbox name not allowed";
245
0
    case SmtpStatusCode::TRANSACTION_FAIL:
246
0
      return "Transaction failed";
247
0
    case SmtpStatusCode::DOMAIN_NOT_ACCEPT:
248
0
      return "Domain does not accept mail";
249
0
    default:
250
0
      return "Unknown status code";
251
0
    }
252
0
  }
253
254
  std::string SmtpResponseLayer::toString() const
255
0
  {
256
0
    return "SMTP response layer, status code: " + getStatusCodeAsString(getStatusCode());
257
0
  }
258
259
  const FieldDescriptor SmtpResponseLayer::SerializedFields::StatusCode{ Layer::SerializedFields::MaxID + 1,
260
                                                                       "statusCode" };
261
  const FieldDescriptor SmtpResponseLayer::SerializedFields::StatusCodeDescription{
262
    Layer::SerializedFields::MaxID + 2, "statusCodeDescription"
263
  };
264
  const FieldDescriptor SmtpResponseLayer::SerializedFields::StatusOption{ Layer::SerializedFields::MaxID + 3,
265
                                                                         "statusOption" };
266
  const FieldDescriptor SmtpResponseLayer::SerializedFields::MultiLine{ Layer::SerializedFields::MaxID + 4,
267
                                                                      "multiLine" };
268
269
  void SmtpResponseLayer::serializeLayer(ObjectScope& serializer) const
270
0
  {
271
0
    serializer.writeField(SerializedFields::StatusCode, static_cast<int>(getStatusCode()));
272
0
    serializer.writeField(SerializedFields::StatusCodeDescription, getStatusCodeAsString(getStatusCode()));
273
0
    serializer.writeField(SerializedFields::StatusOption, getStatusOption());
274
0
    serializer.writeField(SerializedFields::MultiLine, isMultiLine());
275
0
  }
276
277
}  // namespace pcpp